2017-09-11 27 views
-4

我想解析這種類型的數組在swift中。如何解析這樣的json數組在swift中

if let poll_m = JSON?["poll_choices"] as? [String:Any] 
{ 
    print(poll_m) 
} 

但它不工作,請幫助。

"poll_choices" =  (
       { 
      "choice_id" = 155; 
      "choice_text" = 1; 
     }, 
       { 
      "choice_id" = 156; 
      "choice_text" = 2; 
     }, 
       { 
      "choice_id" = 157; 
      "choice_text" = "3\n"; 
     } 
    ); 
+2

「不工作」是什麼意思? – Fogmeister

+0

無法解析。 –

+0

哪裏是「poll_media」? – Lion

回答

1

@Rahul好像你只是想解決辦法,而不是試圖,

隨着人們都說在評論中明確提出「poll_choices」不是字典,但它是一個陣列(也可見)

Parsed Code for you

if let pollChoices = JSON?["poll_choices"] as? [[String:Any]] { //or [Any] 
    for dict in pollChoices { 
     if let choiceId = dict["choice_id"] as? Int, let choiceText = dict["choice_text"] as? String { 
      //Int and String Can also be Any 
      print(choiceId, choiceText) 
     } 
    } 
}