在NSDictionnary中格式化JSON数据

问题描述:

我想从JSON输入中读取并将其放入NSDictionnary中,以便尽可能平滑地对待它。在NSDictionnary中格式化JSON数据

我工作的例子是:

{ “计数”:4, “下一步”:空, “以前”:空, “结果”:[{ “ID”:1, “name”:“Max”},{“id”:2,“name”:“Hugo”},{“id”:3,“name”:“Romain”},{“id”:4, “:”Laurent“}]}

我唯一感兴趣的是”结果“部分。

这是我如何把DATAS一个数组:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"xxxxxx.com/?format=json"]]; 

NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
NSError* err = nil; 
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:&err]; 

jsonArray的结构

的NSArray

http://i.stack.imgur.com/OFRY3.png

我很困惑的键/价值对,我做了一些研究而没有发现任何有价值的东西。我试过的是:

NSArray *values = [[jsonArray objectAtIndex:0] objectAtIndex:1]; 
    for(NSDictionary *item in values) { 
     NSLog(@"%@ --- %@", item[@"id"], item[@"name"]); 

但是,我明显得到一个错误,因为格式不好。我是Objective C的新手,我仍然在寻找网络来找到合适的答案,但非常感谢。

非常感谢!

看起来你的JSON是包含数组的词典:

NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:&err]; 

// then get your array 
NSArray *results = jsonDict[@"results"]; 
// then print 
for (NSDictionary *dict in results) { 
    NSLog(@"id: %@, name: %@", dict[@"id"], dict[@"name"]); 
}