Parsing a JSON array with dictionaries

11,023

Solution 1

NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSLog(@"%@", dataDictionary);
//NSArray *ar = [NSArray arrayWithObject:dataDictionary];//REMOVED
for (NSString *key in [dataDictionary allKeys]) {
    NSDictionary *dic=[dataDictionary objectForKey:key];//ADDED
    for (NSString *dickey in [dic allKeys]) { //MODIFIED
        NSDictionary *dict=[dic objectForKey:dicKey];//ADDED
        TickerData *t=[[TickerData alloc] init];//ALLOC INIT ?
        t.currency = key;//EDITED
        t.symbol = [dict objectForKey:@"symbol"];
        t.last = [dict objectForKey:@"last"];
        [_tickerArray addObject:t];
    }

}

Your data doesn't contain any array, its all dictionaries, try the above code see comments too..

Hope it works..

Edited:

Yes you have initialize the object too, as suggested above in other answers..

Solution 2

t is nil, you have to alloc/ init it:

TickerData *t = [[TickerData alloc] init];
Share:
11,023
cherbear
Author by

cherbear

Updated on June 14, 2022

Comments

  • cherbear
    cherbear almost 2 years

    I'm having some trouble getting to the data I want to in the JSON file. Here is a shortened version of the output from my console:

    {
        AUD =     {
            15m = "125.15547";
            24h = "124.74";
            buy = "121.0177";
            last = "125.15547";
            sell = "123.44883";
            symbol = "$";
        };
        BRL =     {
            15m = "120.34";
            24h = "120.34";
            buy = "120.34";
            last = "120.34";
            sell = "120.34";
            symbol = "R$";
        };
        CAD =     {
            15m = "129.08612";
            24h = "131.07";
            buy = "128.66227";
            last = "129.08612";
            sell = "129.08612";
            symbol = "$";
        };
    }
    

    I'm trying to parse the file using the built in JSON parsing library. Here is the parser in my viewDidLoad method:

        _tickerArray = [NSMutableArray array];
    
        NSURL *tickerDataURL = [NSURL URLWithString:@"https://blockchain.info/ticker"];
        NSData *jsonData = [NSData dataWithContentsOfURL:tickerDataURL];
        NSError *error = nil;
    NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
        NSLog(@"%@", dataDictionary);
        NSArray *ar = [NSArray arrayWithObject:dataDictionary];
        for (NSString *key in [dataDictionary allKeys]) {
            for (NSDictionary *dict in ar) {
                TickerData *t;
                t.currency = [dict objectForKey:key];
                t.symbol = [dict objectForKey:@"symbol"];
                t.last = [dict objectForKey:@"last"];
                [_tickerArray addObject:t];
            }
    
        }
    

    I want to store the currency code (like AUD or BRL) into t.currency along with some of the other data contained in the currency dictionary but now my app is crashing. Error code:

    NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil
    

    None of the objects seem to get added to the _tickerArray

    Help?

    EDIT: Getting the keys to display with the proper data populating other fields:

     NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
        NSLog(@"%@", dataDictionary);
    
    
        for (NSString *key in [dataDictionary allKeys]) {
            NSDictionary *dic=[dataDictionary objectForKey:key];
                TickerData *t=[[TickerData alloc] init];
                t.currency = key;//EDITED
                t.symbol = [dic objectForKey:@"symbol"];
                t.last = [dic objectForKey:@"last"];
                [_tickerArray addObject:t];
    
        }
    
  • cherbear
    cherbear about 11 years
    It seems to work better than the code I had. Though I now get an error __NSCFNumber objectForKey:]: unrecognized selector sent to instance 0x7184550 with that instance being dict which according to my console is getting it's value from 15m in the JSON file
  • iphonic
    iphonic about 11 years
    Debug and try to correct, my code is untested though.. But your code will not work what you are trying to do..
  • cherbear
    cherbear about 11 years
    Well this doesn't cause a crash which is nice. But for some reason none of the labels that I put in my TableView seem to be getting the data. But everything is displaying properly in my console since I added an NSLOG outside of the for statements. Thanks!
  • Martin R
    Martin R about 11 years
    +1! Small correction: t.currency = [dict objectForKey:key] should probably be t.currency = [dict objectForKey:@"symbol"]
  • cherbear
    cherbear about 11 years
    @MartinR but I'm trying to get the Top-level dictionary name into t.currency not the symbol. For some reason I still can't get the data to display in the table. It does however display in my console
  • cherbear
    cherbear about 11 years
    @iphonic that last edit did it! The key now displays in my table. Thank you so much!
  • Martin R
    Martin R about 11 years
    Note that you can simplify for (NSString *key in [dataDictionary allKeys]) to for (NSString *key in dataDictionary).