使用字典解析JSON数组 [英] Parsing a JSON array with dictionaries

查看:96
本文介绍了使用字典解析JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在获取JSON文件中想要的数据时遇到了一些麻烦。以下是我的控制台输出的缩短版本:

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 = "$";
    };
}

我正在尝试使用内置的JSON解析库解析文件。这是我的 viewDidLoad 方法中的解析器:

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

    }

我想存储货币代码(如 AUD或BRL )到 t.currency 以及货币词典中包含的其他一些数据但现在我的应用程序崩溃了。
错误代码:

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

似乎没有任何对象被添加到 _tickerArray

None of the objects seem to get added to the _tickerArray

帮助?

编辑:使用正确的数据填充显示键其他字段:

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

    }


推荐答案

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..

希望它有效..

已编辑:

是的,您也已按照上面其他答案中的建议初始化对象..

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

这篇关于使用字典解析JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆