数组中的元素来自Json Api

时间:2014-01-05 14:44:25

标签: objective-c arrays json xcode api

我想将所有价值从“价格”收集到数组中,然后计算,但遗憾的是我一直在数组中获得0项。请宽大地对待obj-c。

        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]);

1 个答案:

答案 0 :(得分: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.