JSON语法问题

时间:2016-05-20 23:21:30

标签: php json jsonp

我不熟悉JSON,我主要做PHP。当我对Mapquest地理编码API运行查询时,我得到以下json字符串,当尝试在PHP中对其进行json_decode时,该字符串变为“NULL”。

renderOptions({
    "info": {
        "statuscode": 0,
        "copyright": {
            "text": "\u00A9 2016 MapQuest, Inc.",
            "imageUrl": "https://api.mqcdn.com/res/mqlogo.gif",
            "imageAltText": "\u00A9 2016 MapQuest, Inc."
        },
        "messages": []
    },
    "options": {
        "maxResults": -1,
        "thumbMaps": true,
        "ignoreLatLngInput": false
    },
    "results": [{
        "providedLocation": {
            "street": "Kingston Upon Thames,uk"
        },
        "locations": [{
            "street": "",
            "unknownInput": "",
            "type": "s",
            "latLng": {
                "lat": 51.409628,
                "lng": -0.306262
            },
            "displayLatLng": {
                "lat": 51.409628,
                "lng": -0.306262
            },
            "mapUrl": "https://open.mapquestapi.com/staticmap/v4/getmap?key=na&type=map&size=225,160&pois=purple-1,51.4096275,-0.3062621,0,0,|¢er=51.4096275,-0.3062621&zoom=12&rand=54353"
        }]
    }]
})

通过JSONLint运行它,我收到以下错误:

  

错误:第1行的解析错误:renderOptions({“in ^ Expecting   'STRING','NUMBER','NULL','TRUE','FALSE','{','[',''undefined'

我认为这个修复非常简单,但是我不太了解JSON语法,所以我一直在把它放在'renderOptions'之前。

解决问题的正确语法是什么?

3 个答案:

答案 0 :(得分:1)

删除renderOptions(),这些括号中的内容是JSON,从第一个卷曲开始到最后一个卷曲结束。

答案 1 :(得分:0)

这将是一个有效的json

{
    "renderOptions": {
        "info": {
            "statuscode": 0,
            "copyright": {
                "text": "\u00A9 2016 MapQuest, Inc.",
                "imageUrl": "https://api.mqcdn.com/res/mqlogo.gif",
                "imageAltText": "\u00A9 2016 MapQuest, Inc."
            },
            "messages": []
        },
        "options": {
            "maxResults": -1,
            "thumbMaps": true,
            "ignoreLatLngInput": false
        },
        "results": [{
            "providedLocation": {
                "street": "Kingston Upon Thames,uk"
            },
            "locations": [{
                "street": "",
                "unknownInput": "",
                "type": "s",
                "latLng": {
                    "lat": 51.409628,
                    "lng": -0.306262
                },
                "displayLatLng": {
                    "lat": 51.409628,
                    "lng": -0.306262
                },
                "mapUrl": "https://open.mapquestapi.com/staticmap/v4/getmap?key=na&type=map&size=225,160&pois=purple-1,51.4096275,-0.3062621,0,0,|¢er=51.4096275,-0.3062621&zoom=12&rand=54353"
            }]
        }]
    }
}

答案 2 :(得分:0)

在您的mapquest地理编码服务的网址中,您将拥有一个参数" callback = renderOptions" - 可能复制&从一个例子粘贴。删除该参数以删除回调包装。