1

To parse a JSON, as I found also on the web, I usually used this kind of code:

guard let results = receivedUserJSON["results"] as? [String: Any] else {
    print("Error interpreting results")
    return
}

This time I have a problem, because it seems to end in the else of this guard let. The JSON has the following structure:

{
    "results": [{
        "gender": "female",
        "name": {
            "title": "mrs",
            "first": "silene",
            "last": "almeida"
        },
        "location": {
            "street": "2594 rua maranhão ",
            "city": "pouso alegre",
            "state": "distrito federal",
            "postcode": 20447,
            "coordinates": {
                "latitude": "-70.0198",
                "longitude": "123.6577"
            },
            "timezone": {
                "offset": "+4:30",
                "description": "Kabul"
            }
        },
        "email": "[email protected]",
        "login": {
            "uuid": "d06a46b3-1c00-42be-b8fc-d271bf901f7d",
            "username": "silversnake251",
            "password": "ventura",
            "salt": "UcckU6RG",
            "md5": "7c8c4129587c61da01ca7cf4f88353c5",
            "sha1": "6cbf7ec377ff4ebad5a392ec487343bf613858ef",
            "sha256": "8dedf3649fb833a1936b8885627b86c6cf02062eb74f727b2cbd674a30f73e75"
        },
        "dob": {
            "date": "1969-07-13T00:58:26Z",
            "age": 49
        },
        "registered": {
            "date": "2003-09-28T09:44:56Z",
            "age": 15
        },
        "phone": "(95) 0094-8716",
        "cell": "(20) 1014-3529",
        "id": {
            "name": "",
            "value": null
        },
        "picture": {
            "large": "https://randomuser.me/api/portraits/women/66.jpg",
            "medium": "https://randomuser.me/api/portraits/med/women/66.jpg",
            "thumbnail": "https://randomuser.me/api/portraits/thumb/women/66.jpg"
        },
        "nat": "BR"
    }],
    "info": {
        "seed": "dd971cddf636d2d7",
        "results": 1,
        "page": 1,
        "version": "1.2"
    }
}

What should I do to properly parse this JSON? I would prefer not to go for the Codable solution because I don't need all of these values.

PS: I know the json is correct because I tried and printed it with:

if let JSONString = String(data: responseData, encoding: String.Encoding.utf8) {
    print(JSONString)
}
3
  • Put a break point at the Guard Else and post the value of receivedUserJSON["results"] at that time Commented Oct 22, 2018 at 16:49
  • 1
    The Json is valid as you have mentioned. The problem is your results is an array of [String: Any] and you are parsing it as [String: Any]. Try, guard let results = receivedUserJSON["results"] as? [[String: Any]] else { print("Error interpreting results") return } and this should get you the results. Commented Oct 22, 2018 at 16:51
  • Please look into using Swift Codable and actual models.. Commented Oct 23, 2018 at 0:17

1 Answer 1

2

results is an array

guard let results = receivedUserJSON["results"] as? [[String:Any]] else {
            print("Error interpreting results")
            return
        }

I see no value for it to be an array as it contains 1 element so you may think to alter this json

current strucsture

{
    "results": [{}],
    "info": {
        "seed": "dd971cddf636d2d7",
        "results": 1,
        "page": 1,
        "version": "1.2"
    }
}

you may alter it to

{
    "results": {},
    "info": {
        "seed": "dd971cddf636d2d7",
        "results": 1,
        "page": 1,
        "version": "1.2"
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

This solved it! Thank you very much. Can't change the structure of the JSON because I'm using some APIs though, thank you very much!
@Garu94 you may think of using Codable as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.