如何解析这种类型的字典?

问题描述 投票:-1回答:3

如何解析“interestpolls”字典,我想在数组中追加这个poll_id。

我是通过这种方式尝试但它没有工作;建议解析这本词典的方法。

if let result = JSON?["result"] as? String
                    {
                        if result == "success"
                        {
                                if let n=JSON?["interestpolls"] as? [String:Any]
                                {

//parsing code
}
}
}

如何在Swift中解析这个字典:

 {
      "result": "success",
      "err_message": "Polls found successfully",
      "err_code": "E100",
      "interestpolls": [
        {
          "rank": 4,
          "poll_id": 49,
          "poll_text": "Video Image Poll ?",
          "poll_type": "S",
          "user_id_creator": 29,
          "user_full_name": "Sam",
          "profile_pic": "https://pollyscrackers.s3.amazonaws.com/users/profile_pic_29_1503498846.jpg",
          "poll_visibility": "W",
          "dt_created": 1503988510696,
          "poll_media_url": "https://pollyscrackers.s3.amazonaws.com/polls/poll_49_1503988510.jpg",
          "poll_results": {
            "poll_results_vote_based": {
              "totalvotes": 3,
              "Yes": 3,
              "No": 0
            }
          }
        },
        {
          "rank": 1,
          "poll_id": 6,
          "poll_text": "New Poll",
          "poll_type": "S",
          "user_id_creator": 10,
          "user_full_name": "Mohan Roy vaghela",
          "profile_pic": "https://pollyscrackers.s3.amazonaws.com/users/profile_pic_10_1504077441.gif",
          "poll_visibility": "W",
          "dt_created": 1501746922046,
          "poll_media_url": "https://pollyscrackers.s3.amazonaws.com/polls/poll_6_1501746922.jpg",
          "poll_results": {
            "poll_results_vote_based": {
              "totalvotes": 14,
              "Answer1": 7,
              "Answer2": 6
            }
          }
        },
        {
          "rank": 2,
          "poll_id": 28,
          "poll_text": "What is the way to become enterprenur",
          "poll_type": "S",
          "user_id_creator": 1,
          "user_full_name": "Akshay",
          "profile_pic": "https://pollyscrackers.s3.amazonaws.com/users/profile_pic_1_1504503078.jpg",
          "poll_visibility": "W",
          "dt_created": 1501746922046,
          "poll_media_url": "https://pollyscrackers.s3.amazonaws.com/polls/poll_8_1502198168.jpg",
          "poll_results": {
            "poll_results_vote_based": {
              "totalvotes": 4,
              "Startup with job": 1,
              "Startup without job": 3
            }
          }
        },
        {
          "rank": 3,
          "poll_id": 29,
          "poll_text": "Who is best prgrammer in world",
          "poll_type": "S",
          "user_id_creator": 1,
          "user_full_name": "Akshay",
          "profile_pic": "https://pollyscrackers.s3.amazonaws.com/users/profile_pic_1_1504503078.jpg",
          "poll_visibility": "W",
          "dt_created": 1501746922046,
          "poll_media_url": "https://pollyscrackers.s3.amazonaws.com/polls/poll_8_1502198168.jpg",
          "poll_results": {
            "poll_results_vote_based": {
              "totalvotes": 4,
              "AKS": 2,
              "SASK": 2
            }
          }
        }
      ]
    }
json swift parsing dictionary
3个回答
1
投票

您将其作为兴趣民意调查投射为Dictionary,将其强制转换为数组。请尝试以下代码。

var pollIDList = [String]()

if let result = JSON?["result"] as? String {
  if result == "success" {
    let array = JSON?["interestpolls"] as? Array
    pollIDList.append(array["poll_id"])
  }
}

1
投票

试试这个 :

    if let result = JSON?["result"] as? String
{
    if result == "success"
    {
        if let n=JSON?["interestpolls"] as? [[String:Any]]
        {
            for data in n {
                if let id = data["poll_id"] as? String {
                    self.pollIDList.append(id)
                }
            }

        }
    }
}

0
投票

正如“diu”指出的那样,你的interestpolls键/值对包含一个数组,而不是一个字典。

另一点是你可以创建一个复杂的if语句而不是嵌套一大堆ifs:

if let result = JSON?["result"] as? String,
  result == "success",
  let  polls = JSON?["interestpolls"] as? [[String: Any]] {
    for aPollDict in polls {
      //your code to parse a poll here
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.