2016-06-25 13 views
2

構造体にデータを送信するJSON配列をループしようとしています。私は3つの文字列でのエラー「何の添字を持っていない」そうだ、なぜ非常にわからないSwift 3 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オブジェクトを返すためにSwiftyJSONを使用して私のコードです。

ご協力いただきありがとうございます。

EDIT:は、ここにあなたがarrayメソッドを使用する必要があります

{ 
    "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

JSONのサンプルです。このように、あなたのライン

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 
関連する問題