如何在iOS中解析复杂的json数据?

问题描述:

我是iOS新手,我想解析数据,但它非常复杂,我不知道如何解析它。下面给出的是json数据。如何在iOS中解析复杂的json数据?

{ 
    "response_code": 200, 
    "last_updated": { 
     "date": "2015-12-27", 
     "time": "01:32:13" 
    }, 
    "trains": [ 
     { 
      "train": { 
       "number": "04921", 
       "start_time": "04:45", 
       "name": "SRE-UMB MEMU SPECIAL", 
       "type": "HSP" 
      }, 
      "dest": { 
       "code": "UMB", 
       "name": "AMBALA CANT JN" 
      }, 
      "source": { 
       "code": "SRE", 
       "name": "SAHARANPUR" 
      } 
     }, 
     { 
      "train": { 
       "number": "04922", 
       "start_time": "20:45", 
       "name": "UMB-SRE MEMU SPECIAL", 
       "type": "HSP" 
      }, 
      "dest": { 
       "code": "SRE", 
       "name": "SAHARANPUR" 
      }, 
      "source": { 
       "code": "UMB", 
       "name": "AMBALA CANT JN" 
      } 
     } 
    ] 
} 
+1

你的感觉是在这个复杂......你能不能详细 –

+0

'trains'是一个数组,'火车,蒸馏水和source'是字典 –

+0

[检查此链接](http://stackoverflow.com/questions/33123797/swift-could-not-cast-value-of-type-nscfarray-to-nsdictionary/33123953#33123953)更好的主意 –

{ , , } - 这是一本字典
[ , , ] - 它是一个数组

你的情况,你会得到:

第一级 - 字典,键response_codelast_updatedtrains
其中:
response_code - 价值
last_updated - 字典,键datetime
trains - 阵列键traindest词典,source

使用NSJSONSerialization类,很容易。

例如,在Objective-C:

id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error]; 
if (jsonObject) { 
    // jsonObject is an NSArray or NSDictionary representation of the data that you can now do something with 
}