2013-06-03 53 views
0

我需要解析由Google Maps API 提供的JSON文件然后我使用AFNetworkingRequestHow获取JSON响应。 我建这本字典:如何解析JSON多阵列

NSDictionary *jsonDict = (NSDictionary *) JSON; 
    NSArray *rows = [jsonDict objectForKey:@"rows"]; 

但现在,我怎么能达到时间标签的第一场?

JSON文件解析:

{ 
"destination_addresses" : [ "San Francisco, CA, USA", "Victoria, BC, Canada" ], 
"origin_addresses" : [ "Vancouver, BC, Canada", "Seattle, WA, USA" ], 
"rows" : [ 
    { 
     "elements" : [ 
     { 
      "distance" : { 
       "text" : "1,527 km", 
       "value" : 1527251 
      }, 
      "duration" : { 
       "text" : "15 hours 0 mins", 
       "value" : 54010 
      }, 
      "status" : "OK" 
     }, 
     { 
      "distance" : { 
       "text" : "112 km", 
       "value" : 111906 
      }, 
      "duration" : { 
       "text" : "3 hours 1 min", 
       "value" : 10885 
      }, 
      "status" : "OK" 
     } 
     ] 
    } 
} 
+0

检查[这里的技术(http://stackoverflow.com/questions/14958883/ios-serialize-deserialize- complex-json-general-from-nsobject-class/16771574#16771574),这是更通用,更不容易出错。 –

回答

1

尝试

NSDictionary *jsonDict = (NSDictionary *) JSON;; 

NSArray *rows = jsonDict[@"rows"]; 
NSDictionary *row = rows[0]; 

NSArray *elements = row[@"elements"]; 
NSDictionary *element = elements[0]; 

NSDictionary *duration = element[@"duration"]; 
NSInteger value = [duration[@"value"] integerValue]; 
+1

谢谢你的朋友:) –

0
[json objectForKey:@"elements"] 

是一个NSArray。您无法直接使用数组初始化NSDictionary。

(在JSON,{和}表示键值对,喜欢的NSDictionary,而[和]划有序像NSArray的列表,这就是为什么映射完成,因为它是...)

继能认为你了解开发时间,这可能会有很大的帮助。

+0

对不起,但NSDictionary不是用数组初始化的。 –

+0

您需要从数组中获取第一个对象,然后应用objectForKey。 –

0

您可以使用下面的代码:

NSString *valueString = [[[[[rows objectAtindex:0]objectForKey:@"elements" ]objectAtindex:0]objectForKey:@"duration"] objectForKey:@"value"]; 

//if you want integer value 
NSInteger value = [valueString integerValue];