找到你要的答案

Q:swift json error dankogai/swift-json

Q:斯威夫特JSON错误dankogai /斯威夫特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){
....
}

我用dankogai /斯威夫特JSON从Web服务获取JSON响应..

一切都进行得很顺利,但有时Web服务不能给我回复,因为数据库中没有数据。这不是问题。

但我必须处理时,我得到“零”从网络服务。

这是我的JSON请求:

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..

谢谢您!

用这个解决:

if (json["bkkMainPrTable"].asError == nil){
....
}
answer1: 回答1:

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

当然,我可以帮助。

我打了个电话:

HTTP:/ / 79.172.249.175:7001 / restwebserviceapp /网络资源/ entity.bkkmainprtable/47.490477/19.030486

得到反应:

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

把它放在那里:

http://jsonlint.com/

得到了结果:

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

有效的JSON

json  swift