使用字典解析JSON数组

编程入门 行业动态 更新时间:2024-10-23 10:32:42
本文介绍了使用字典解析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:@"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数组

本文发布于:2023-11-26 03:08:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632391.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字典   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!