-2

我在 json 中有以下响应,我不确定如何解析它

[{"WordArray":[{"Id":11,"Word1":"ODDER","Category":"Dyr","Differculty":1,"Language":"Danish"},{"Id":225,"Word1":"PRAG","Category":"International by","Differculty":1,"Language":"Danish"}]},{"WordArray":[{"Id":3,"Word1":"TIGER","Category":"Dyr","Differculty":1,"Language":"Danish"},{"Id":154,"Word1":"BOLLE","Category":"Mad og drikke","Differculty":1,"Language":"Danish"}]},
{"WordArray":[{"Id":3,"Word1":"TIGER","Category":"Dyr","Differculty":1,"Language":"Danish"},{"Id":147,"Word1":"PIZZA","Category":"Mad og drikke","Differculty":1,"Language":"Danish"}]}]

我试过以下没有运气

NSMutableArray* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

NSMutableArray * roundWords = [[NSMutableArray alloc]retain ];

roundWords = [json objectAtIndex:i];

json 包含我的 3 'WordArray' 但我不确定如何继续.. 例如,如何遍历每个项目WordArray并 NSLogWord1值?

谢谢!

4

1 回答 1

1

我如何遍历 WordArray 和 NSLog 中的每个项目 Word1 值

使用快速枚举。

for (NSDictionary *dict in array) {
    NSLog(@"%@", [dict objectForKey:@"Word1"]);
}

顺便说一句,您的代码在几个地方是错误的:

NSMutableArray *roundWords = [[NSMutableArray alloc] retain];
roundWords = [json objectAtIndex:i];

一,不使用这样retain的chained with alloc。用于init初始化对象。

二:除非您使用 ARC(您不使用),否则您将使用该行泄漏内存 - 指向原始数组的指针被丢弃,因为您重新分配了其他内容。

三:[json objectAtIndex:i]不是NSMutableArray- 它是一个NSDictionary(顺便说一句,它是不可变的。声明不是魔法 - 但是你声明你的变量,它不会影响NSJSONSerialization返回不可变字典的事实,除非另有说明)。

于 2013-02-27T18:24:52.600 回答