2016-06-28 132 views
1

我想解析JSON字符串如下,我wondring如何打印[時間]值斯威夫特:解析JSON字符串無鑰匙

JSON字符串:

{ 
date: "2014-08-13", 
method: "Makkah: Umm al-Qura University, Makkah", 
latitude: "30.0599153", 
longtude: "31.2620199", 
timezone: "+3", 
times: [ 
"04:52", 
"06:21", 
"13:00", 
"16:36", 
"19:38", 
"19:38", 
"21:08" 
] } 

這是我的代碼:

let json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments) 

       let todayDate = json["date"] 
       let method = json["method"] 
       let latitude = json["latitude"] 
       let longtude = json["longtude"] 
       let timezone = json["timezone"] 

所以問題是,如何打印時代,感謝

回答

3
let times = json["times"] as! [String] 

for time in times { 
    print(time) 
} 

或在一行

times.forEach{ print($0) } 
+0

它的偉大工程,由於Vadian先生 –

+1

爲了澄清,時間是一個數組。您發佈的JSON是一個包含各種鍵/值對的字典。其中之一是關鍵的「時間」,並且該關鍵字的值是一串時間字符串。 –