sulabh
sulabh

Reputation: 249

JSON Parsing issue in swift 3 from Array to dictionary

i have a JSON as:

{
  "jsonData": {
    "userDetails": [
      {
        "user_id": "a",
        "first_name": "First1",
        "last_name": "Last1",
        "donation_amount": 841,
        "donation_time": 1452678347523
      },
      {
        "user_id": "b",
        "first_name": "First2",
        "last_name": "Last2",
        "donation_amount": 841,
        "donation_time": 1452678347523
      },
      {
        "user_id": "c",
        "first_name": "First3",
        "last_name": "Last3",
        "donation_amount": 841,
        "donation_time": 1452678347523
      }
    ]
  },
  "total_count": 3
}

and i am parsing in swift using swiftlyJSON : my code is below

    private func processProjectDonorsResponse(response: JSON) {
     //add  to  dictionary
        let jsonObject = response.dictionaryObject!["jsonData"]! as AnyObject
        let details = jsonObject["userDetails"] as! [AnyObject]
        var tempModel = [UserModel]()
        for detail in details {
          let user = UserModel(response: detail as! [String: AnyObject])
          tempModel.append(user)
        }
}

it was working fine in swift 2 , but now i have upgraded to swift 3 i am getting warning at line //let details = jsonObject["userDetails"] as! [AnyObject] as: Cast from String ?! to unrelated type '[AnyObject]' always fail., and gets crashed. how to fix this problem?

Upvotes: 1

Views: 2642

Answers (1)

Ashish Verma
Ashish Verma

Reputation: 1818

The problem in your code is in the following line:

let jsonObject = response.dictionaryObject!["jsonData"]! as AnyObject

Just change this code with the following:

let jsonObject = response.dictionaryObject!["jsonData"]! as! [String: AnyObject]

Hope, this will work for you.

Upvotes: 1

Related Questions