-1

I am trying to get the latest updated entry in my JSON http://data.mtgox.com/api/1/BTCUSD/trades/fetch but unfortunately the latest updated entries appear at the bottom and not at the top of the JSON list. Here is my code

- (void)fetchedlatestData:(NSData *)responseData {
    NSError* error;
    NSDictionary* json = [NSJSONSerialization
                          JSONObjectWithData:responseData //1
                          options:kNilOptions
                          error:&error];


    NSArray* bitcoinPrices2 = [json objectForKey:@"return"]; //2

    //Get the Last Price
    NSDictionary* latest = [bitcoinPrices2 objectAtIndex:0];
    NSDictionary* latestPrice = [latest objectForKey:@"price"];



    NSLog(@"Data: %@", latestPrice);

By using objectAtIndex:0I can get the first entry, but how would I go about getting the very last entry as the index number would not be consistent and the amount of entries in the JSON is not a consistent number. Would I need to reverse the order of the JSON when it comes in so the index is 0? How would I go about this? I am trying to get the last two "price" numbers to compare them and I am new to how JSON works. Thanks

4

3 回答 3

3

您的项目在一个数组中。您可以使用该count方法获取数组中的项目数。一旦你有计数,你可以得到你想要的任何项目。lastObject也是获取最后一项的快捷方式。

    NSDictionary* last = [bitcoinPrices2 lastObject];
    NSDictionary* secondLast = [bitcoinPrices2 objectAtIndex:(bitcoinPrices2.count - 2)];
于 2013-07-27T23:20:31.110 回答
1

您可以使用-lastObject

NSDictionary* latest = [bitcoinPrices2 lastObject];
于 2013-07-27T23:15:21.867 回答
0

你也可以使用 [bitcoinPrice2 count]。

if ([bitcoinPrice2 count] > 0)
    NSDictionary *lastItem = [bitcoinPrice2 objectAtIndex:([bitcoinPrice2 count] - 1)];
if ([bitcoinprice2 count] > 1)
    NSDictionary *penultimateItem = [bitcoinPrice2 objectAtIndex:([bitcoinPrice2 count] - 2)];
于 2013-07-27T23:19:56.480 回答