2016-06-25 2 views
2

구조체에 데이터를 보내는 JSON 배열을 반복하려고합니다. 나는 세 개의 문자열에 대한 오류 "더 첨자가 없다"는군요 왜 아주 확실하지 않다스위프트 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를 사용하여 내 코드입니다.

도움을 주시면 감사하겠습니다.

편집 : 다음은 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