Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

ios - Parsing a JSON array with dictionaries

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

    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

TickerData *t = [[TickerData alloc] init];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...