solarenqu
solarenqu

Reputation: 814

swift json error dankogai/swift-json

I'm using dankogai/swift-json to get a json response from a web service..

Everything is going fine but, sometimes the web service can't give me back any response, because there is no data in the database. It's not a problem.

But I have to handle when I get "null" from the web service.

This is my json request:

let json = JSON(url:"http://79.172.249.175:7001/RestWebServiceApp/webresources/entity.bkkmainprtable/"+lat+"/"+lon)

//  json is  valid json object... 
if (json["bkkMainPrTable"]["routeShName"].isArray){


    for (k, v) in json["bkkMainPrTable"] {

        colors.append(v["routeShName"].description + " - " +  v["stopName"].description)

    }
    }
  //  json is not a valid json object... 
else { 

        colors.append("Nincs elérhető járat")
        sendKallerBtn.setTitle("No Post", forState: UIControlState.Normal)

    }

But, it is always nil.. and alway step into else.. Can anybody help me how to check is this a valid json object or not..

Thank you!

Solved with this:

if (json["bkkMainPrTable"].asError == nil){
....
}

Upvotes: 0

Views: 90

Answers (1)

kostek
kostek

Reputation: 801

Sure, I can help.

I've made a call:

http://79.172.249.175:7001/RestWebServiceApp/webresources/entity.bkkmainprtable/47.490477/19.030486

got the response:

{"bkkMainPrTable":[{"id":"7857","routType":"3","routeShName":"178","stopId":"F00002","stopLat":"47.490477","stopLatStirng":"47.490477","stopLon":"19.030486","stopLonString":"19.030486","stopName":"Zsolt utca"},{"id":"7954","routType":"3","routeShName":"105","stopId":"F00087","stopLat":"47.496422","stopLatStirng":"47.496422","stopLon":"19.03071","stopLonString":"19.03071","stopName":"Krisztina tĂŠr"},{"id":"7946","routType":"0","routeShName":"18","stopId":"F00080","stopLat":"47.493779","stopLatStirng":"47.493779","stopLon":"19.038183","stopLonString":"19.038183","stopName":"DĂłzsa GyĂśrgy tĂŠr"},{"id":"7943","routType":"3","routeShName":"916","stopId":"F00077","stopLat":"47.494777","stopLatStirng":"47.494777","stopLon":"19.037665","stopLonString":"19.037665","stopName":"DĂłzsa GyĂśrgy tĂŠr"}]}

put it there:

http://jsonlint.com/

and got the result:

{
    "bkkMainPrTable": [
        {
            "id": "7857",
            "routType": "3",
            "routeShName": "178",
            "stopId": "F00002",
            "stopLat": "47.490477",
            "stopLatStirng": "47.490477",
            "stopLon": "19.030486",
            "stopLonString": "19.030486",
            "stopName": "Zsolt utca"
        },
        {
            "id": "7954",
            "routType": "3",
            "routeShName": "105",
            "stopId": "F00087",
            "stopLat": "47.496422",
            "stopLatStirng": "47.496422",
            "stopLon": "19.03071",
            "stopLonString": "19.03071",
            "stopName": "Krisztina tĂŠr"
        },
        {
            "id": "7946",
            "routType": "0",
            "routeShName": "18",
            "stopId": "F00080",
            "stopLat": "47.493779",
            "stopLatStirng": "47.493779",
            "stopLon": "19.038183",
            "stopLonString": "19.038183",
            "stopName": "DĂłzsa GyĂśrgy tĂŠr"
        },
        {
            "id": "7943",
            "routType": "3",
            "routeShName": "916",
            "stopId": "F00077",
            "stopLat": "47.494777",
            "stopLatStirng": "47.494777",
            "stopLon": "19.037665",
            "stopLonString": "19.037665",
            "stopName": "DĂłzsa GyĂśrgy tĂŠr"
        }
    ]
}

Valid JSON

Upvotes: 1

Related Questions