2016-06-25 97 views
2

我正試圖通過發送數據到結構的JSON數組循環。Swift 3循環JSON數據

這裏是我的代碼,使用SwiftyJSON返回一個JSON對象:

performAPICall() { 
    json in 
    if(json != nil){ 
    print("Here is the JSON:") 
    print(json["content"]["clients"]) 

     let clients = json["content"]["clients"] 
     for client in clients { 
      var thisClient = Client() 
      thisClient.id = client["id"].string 
      thisClient.desc = client["desc"].string 
      thisClient.name = client["name"].string 
      self.clientArray.append(thisClient) 
    } 
    self.tableView.reloadData() 
    } else { 
     print("Something went very wrong..,") 
    } 
} 

我不明白爲什麼我得到「無標」的錯誤在三根弦。

任何幫助表示讚賞,謝謝。

編輯:這裏的JSON

{ 
    "content": { 
     "clients": [{ 
      "group": "client", 
      "id": "group_8oefXvIRV4", 
      "name": "John Doe", 
      "desc": "John's group." 
     }, { 
      "group": "client", 
      "id": "group_hVqIc1eEsZ", 
      "name": "Demo Client One", 
      "desc": "Demo Client One's description! " 
     }, { 
      "group": "client", 
      "id": "group_Yb0vvlscci", 
      "name": "Demo Client Two", 
      "desc": "This is Demo Client Two's group" 
     }] 
    } 
} 
+0

你能後的JSON對象? – CodeBender

+0

我已將它添加到問題中。 – Aloogy

+0

不幸的是,我忘了包含頂級的「頂級」級別,但是如果我只是打印(json),那麼它就很難對付。 – Aloogy

回答

1

您應該使用array方法的樣本。因此,您的線路

let clients = json["content"]["clients"] 

應該使用array(安全地解開吧):

guard let clients = json["content"]["clients"].array else { 
    print("didn't find content/clients") 
    return 
} 

// proceed with `for` loop here