2017-05-30 37 views
0

如何將此數據解析成swift3並顯示成的UILabel。試圖JSON解析成swift3並顯示成的UILabel

請別人幫我度過這個,因爲我是新來的迅速和我的第一個項目。提前致謝。

{ 
    「Driver_Details」 :[ 
    { 
     「Staff_ID」:2, 
     「Staff_Name」:」Pratyush」, 
     「Student_ID」:1 
     「Route_Number」:」1A」 
     「Route_Name」:」Guindy」 
     「Stop_Name」:」Velachery」 
     「Distance_from_School」:3, 
    } 
] 
} 
+1

你能提供你的swift部分代碼嗎? – FieryCat

+0

我還沒有嘗試過,因爲我沒有從我應該開始那裏知道的請幫我:( – BugFinder

+0

然後,請首先檢查教程。贊(直接有關JSON /斯威夫特)http://roadfiresoftware.com/2016/ 12 /如何對語法分析JSON-與-迅速-3 /或(一般情況下)https://stackoverflow.com/documentation/swift/topics – FieryCat

回答

0

試試這個。如果你打開這個文件夾作爲DriverDetails.json的本地文件,你可以試試這個:

if let path = Bundle.main.path(forResource: "DriverDetails", ofType: "json") { 
    do { 
     let jsonData = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe) 
     if let json = try JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers) as? [String: Any] { 
      if let jsonDriverDetails = json["Driver_Details"] as? [[String: Any]] { 
       // Here you have an array of DriverDetails Dictionaries 
       for jsonDriverDetail in jsonDriverDetails { 
        // Iterating through each Dictionary you can access the values using the key 
        staffNameLabel.text = jsonDriverDetails["Staff_Name"] as? String 
       } 
      } 
     } 
    } catch { print(error.localizedDescription) } 
} 
+0

嘿感謝隊友,但這個數據是來自該網址,我應該更改爲您的代碼? – BugFinder

+0

你在使用AlamoFire嗎? – pesch

+0

沒有它的非常簡單的應用程序和數據來自服務器。 – BugFinder