Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何解析JSON多数组_Ios_Json_Afnetworking_Afjsonrequestoperation - Fatal编程技术网

Ios 如何解析JSON多数组

Ios 如何解析JSON多数组,ios,json,afnetworking,afjsonrequestoperation,Ios,Json,Afnetworking,Afjsonrequestoperation,我需要解析由提供的JSON文件 然后我使用AFNetworkingRequestHow获取JSON响应。 我制作了这本字典: NSDictionary *jsonDict = (NSDictionary *) JSON; NSArray *rows = [jsonDict objectForKey:@"rows"]; 但是现在,我如何才能到达duration标记的第一个值字段 要分析的JSON文件: { "destination_addresses" : [ "San Fran

我需要解析由提供的JSON文件 然后我使用AFNetworkingRequestHow获取JSON响应。 我制作了这本字典:

    NSDictionary *jsonDict = (NSDictionary *) JSON;
    NSArray *rows = [jsonDict objectForKey:@"rows"];
但是现在,我如何才能到达duration标记的第一个字段

要分析的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"
         }
      ]
   }   
}
这是一场恐怖袭击。不能直接使用数组初始化NSDictionary

(在JSON中,{和}表示键值对,像NSDictionary一样,而[and]像NSArray一样分隔有序列表,这就是为什么映射是按原样进行的…)

下面的想法可以帮助您了解开发时间,这样可能会有很大帮助。

试试看

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];

您可以使用以下代码:

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

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

很抱歉,NSDictionary未使用数组初始化。为此,您需要从数组中获取第一个对象,然后应用objectForKey。请检查是否更通用且更不容易出错。
NSString *valueString = [[[[[rows objectAtindex:0]objectForKey:@"elements" ]objectAtindex:0]objectForKey:@"duration"] objectForKey:@"value"];

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