iOS - NSArray的嵌套NSDictionaries

时间:2012-04-23 23:14:35

标签: ios nsarray nsdictionary sbjson

我的Json回复是

[{"id":"1", "x":"1", "y":"2"},{"id":2, "x":"2", "y":"4"}]

我做了

 NSString *response = [request responseString];
 SBJSON *parser = [[SBJSON alloc] init];
 NSArray *jsonObject = [parser objectWithString:response error:nil];

此时我相信jsonObject是一个有两个NSDictionary的数组。 如何使jsonObject具有两个NSArray而不是NSDictionary? 这样做的最佳方法是什么?我想我需要将嵌套的NSDictionary转换为NSArray?

1 个答案:

答案 0 :(得分:1)

我现在将在Foundation框架中使用NSJSONSerialization类。

使用JSONObjectWithData方法,它将返回您希望数据存储在顶层的容器类型。例如:

NSError *e;

NSArray *jsonObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:&e]; 

for (NSDictionary *dict in jsonObject) {

    NSLog(@"json data:\n %@", dict); 

    // do stuff

}

或者,您可以返回一个可变容器,例如:

NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&e];

以下是Apple文档:

NSJSONSerialization

相关问题