2017-01-02 7 views
2

Alamofire를 사용하여 Swift에서 JSON 데이터를 사전으로 변환하고 싶습니다. if let dict = response.result.value as? Dictionary<String, AnyObject> { ... }을 시도했지만 불행히도 작동하지 않습니다. 어떤 제안? 미리 감사드립니다. 이것은 당신이 사전의 배열을 만들 필요가 귀하의 응답이 배열이기 때문에 내 response.result.valueAlamofire를 사용하여 Swift에서 JSON을 사전으로 변환

Optional(<__NSArrayI 0x6000002893d0>(
{ 
    category = category1; 
    description = description1; 
    name = sth1; 
    id = 1; 
    price = "213"; 
    type = type1; 
}, 
{ 
    category = category2; 
    description = description2; 
    name = sth2; 
    id = 2; 
    price = "2133"; 
    type = type4; 
}, 
{ 
    category = category3; 
    description = description3; 
    name = sth3; 
    id = 3; 
    price = "21334"; 
    type = type5; 
} 
) 
) 
+0

이유 https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwipqdDNzqPRAhWPN1AKHeXAA7wQFggbMAA&url=https%3A%2F ([ObjectMapper] 사용하지 % 2Fgithub.com % 2FHearst-DD % 2FObjectMapper & usg = AFQjCNFyUn25wAdc4YI3J79e10bh5EsWTQ)? –

답변

1

당신은 당신이 이런 식으로 작업을 수행 할 수 있습니다 단계 ... 하여 JSON 단계에서 데이터를 추출해야합니다.

if let arrayOfDic = response.result.value as? [Dictionary<String,AnyObject>]{ 
     for aDic in arrayOfDic{ 
      print(aDic)//print each of the dictionaries 
      if let price = aDic["price"] as? String{ 
       print(price)//print price of each dic 
      } 
     } 
    } 
3

인쇄의 예입니다. 위의 응답에서는 사전 만 작동하지 않습니다. 그래서 아래와 같이 코드를 변경하십시오.

if let dict = response.result.value as? [[String : AnyObject]] 
{ ... } 
관련 문제