How to parse such json array in swift [closed]

0 votes
asked Sep 11, 2017 by rahul-bandal

I am trying to parse such type of array in swift .

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

but its not working please help.

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

1 Answer

0 votes
answered Sep 11, 2017 by abhishek

@Rahul It seems like you just want solution and not trying,

As peoples are saying on comment clearly "poll_choices" is not dictionary but it is an Array (visible also)

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)
        }
    }
}
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
...