2016-07-16 57 views
-1

我是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)更好的主意 –

回答

0

{ , , } - 這是一本字典
[ , , ] - 它是一個數組

你的情況,你會得到:

第一級 - 字典,鍵response_codelast_updatedtrains
其中:
response_code - 價值
last_updated - 字典,鍵datetime
trains - 陣列鍵traindest詞典,source

0

使用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 
}