Dummy question: I have my json:
let jsonDestination = "[{\"data\": {\"destinos\": [{\"idDestino\": \"1\",\"desDestino\": \"ASUNCION\"},{\"idDestino\": \"2\",\"desDestino\": \"MIAMI\"}]}}]"
And try to print the result if "idDestino" like this:
if let dataFromString = jsonDestination.data(using: .utf8, allowLossyConversion: false) {
let destinationJson = JSON(data: dataFromString)
for item in destinationJson["destinos"].arrayValue {
print(item["idDestino"].stringValue)
}
}
But, never enter to execute the line print(item["idDestino"].stringValue)
Also i try this:
let firtsDestinationId = destinationJson[0]["data"]["destinos"]["idDestino"]
print(firtsDestinationId)
and get this error:
_error NSError? domain: "SwiftyJSONErrorDomain" - code: 901 0x000060000005c080 This error is means: Couldn't merge, because the JSONs differ in type on top level
Same for: let firtsDestinationId = destinationJson["data"][0]["destinos"]["idDestino"]
So..my real problem is i don't know how to catch the data of my json... can any help me here?
PD.: this example work just fine:
let jsonDestination = "{ \"people\": [{ \"firstName\": \"Paul\", \"lastName\": \"Hudson\", \"isAlive\": true }, { \"firstName\": \"Angela\", \"lastName\": \"Merkel\", \"isAlive\": true }, { \"firstName\": \"George\", \"lastName\": \"Washington\", \"isAlive\": false } ] }"
if let dataFromString = jsonDestination.data(using: String.Encoding.utf8) {
let destinationJson = JSON(data: dataFromString)
for item in destinationJson["people"].arrayValue {
print(item["firstName"].stringValue)
}
}
My json is the problem? how to can use correctly?