1

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?

2 Answers 2

1

Look at my comments for each level in your JSON object

[-----------------------------------------Array
   {--------------------------------------Obj
      "data":{----------------------------Obj
         "destinos":[---------------------Array
            {-----------------------------Obj
               "idDestino":"1",
               "desDestino":"ASUNCION"
            },
            {  
               "idDestino":"2",
               "desDestino":"MIAMI"
            }
         ]
      }
   }
]

Your code below doesn't work because JSON element for destinos is an array.

let firtsDestinationId = destinationJson[0]["data"]["destinos"]["idDestino"]

You cant try this following code

let firtsDestinationId = destinationJson[0]["data"]["destinos"][0]["idDestino"]
Sign up to request clarification or add additional context in comments.

Comments

0

For others who want to go through the Json[array]... in my case i write this code:

       for item in destinationJson["data"]["destinos"].arrayValue {
            print(item["idDestino"].stringValue)
        }

Comments

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.