2014-01-05 49 views
0

我想收集所有值從「價格」到數組,並計數,但可悲的是,所有的時間我有0個項目在數組中。如果你對obj-c有所瞭解,請加以寬恕。從Json Api陣列中的元素

 NSData *data = [apiHandler getResponseFromPublicServerUrl:@"https://bitcoinity.org/markets/get_depth_change?currency=USD&exchange=btce&dc_span=10m"]; 

     NSError* error; 
     NSDictionary* json = [NSJSONSerialization 
           JSONObjectWithData:data 
           options:kNilOptions 
           error:&error]; 

     NSArray *resultArray = [json objectForKey:@"price"]; 

     NSLog (@"Number of elements in array = %lu", [resultArray count]); 
+0

'NSDictionary * json'應該是'NSArray * json' ... JSON返回數組... –

回答

0

嘗試以下代碼片段。

NSError* error; 
    id json = [NSJSONSerialization 
          JSONObjectWithData:data 
          options:kNilOptions 
          error:&error]; 


    NSMutableArray *resultArray = [[json valueForKey:@"data"] mutableArrayValueForKeyPath:@"price"]; 
    NSLog (@"Number of element count = %d and Number of elements of array = %@", [resultArray count], resultArray); 

mutableArrayValueForKeyPath method give you all values under key of price.