AFNetworking和NSDictionary JSON索引

时间:2013-12-12 04:04:47

标签: objective-c json nsdictionary afnetworking

如何使用NSDictionary索引编号获取JSON条目?


JSON响应:

{
    "data": {
        "first": [{
            "07:30": "Entry 1"
        }, {
            "09:05": "Entry 2"
        }, {
            "10:40": "Entry 3"
        }],
    },
    "success": true
}

Xcode(AFnetworking):

 [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

      NSMutableArray *myArray = [responseObject objectForKey:@"data"];
      // OK, as NSDictionary have objectForKey.

      NSLog (@"%@", [myArray objectAtIndex:1] );
      // Error ;(
 }];

1 个答案:

答案 0 :(得分:3)

试试这个

[manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

      NSArray *myArray = [[responseObject objectForKey:@"data"] objectForKey:@"first"];

      NSLog (@"%@", [myArray objectAtIndex:1] ); 

 }];

你有类似下面的内容

{                  //<---- Dictionary
    "data": {      //<---- Dictionary
        "first": [ //<---- Array
        {          //<---- Dictionary
            "07:30": "Entry 1"
        }, {
            "09:05": "Entry 2"
        }, {
            "10:40": "Entry 3"
        }],
    },
    "success": true
}