2016-06-25 71 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