This question already has an answer here:
I have an HTTP request that I make, where the response is like so:
{
"results": [
{
"gender": "male",
"name": {
"title": "mr",
"first": "carter",
"last": "roy"
},
"location": {
"street": "8662 pine rd",
"city": "minto",
"state": "nova scotia",
"postcode": "I8Q 4L3",
"coordinates": {
"latitude": "-65.0674",
"longitude": "-174.1034"
},
"timezone": {
"offset": "+8:00",
"description": "Beijing, Perth, Singapore, Hong Kong"
}
},
"email": "carter.roy@example.com",
"login": {...},
"dob": {
"date": "1972-07-18T03:10:43Z",
"age": 46
},
"registered": {
"date": "2014-05-14T07:12:34Z",
"age": 5
},
"phone": "174-802-7804",
"cell": "194-903-4287",
"id": {
"name": "",
"value": null
},
"picture": {
"large": "https://randomuser.me/api/portraits/men/9.jpg",
"medium": "https://randomuser.me/api/portraits/med/men/9.jpg",
"thumbnail": "https://randomuser.me/api/portraits/thumb/men/9.jpg"
},
"nat": "CA"
}
],
"info": {...}
}
The response will always change every time you refresh the page.
The problem is, some fields (like postcode), will either be a string, or int, so how can one specify multiple types for a field like "postcode":
type Location struct {
Street string `json:"street"`
Postcode string `json:"postcode"`
Coordinates Coordinates `json:"coordinates"`
}
Update
This is not a duplicate, because in my case I have two different data types for a field (i.e. string and int for postcode), so how can one specify this in a struct. I highly discourage looping through the raw data just for type checking.
</div>