简体   繁体   中英

How to parse particular JSON in Swift

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": "silene.almeida@example.com",
        "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)
}

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"
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM