在解析json时遇到问题

时间:2019-05-10 03:37:01

标签: php json parsing

我对编码非常陌生。我在网上看了很多例子,但似乎找不到我想要的东西。

https://data.calgary.ca/resource/k7p9-kppz.json

返回的JSON:

[
    {
        ":@computed_region_4a3i_ccfj": "1",
        ":@computed_region_4b54_tmc4": "8",
        ":@computed_region_kxmf_bzkv": "29",
        ":@computed_region_p8tp_5dkv": "5",
        "description": "Glenmore Trail / 14 Street SW (West)",
        "latitude": "50.9938095441965",
        "location": {
            "latitude": "50.9938095441965",
            "longitude": "-114.094978074903",
            "human_address": "{\"address\": \"\", \"city\": \"\", \"state\": \"\", \"zip\": \"\"}"
        },
        "longitude": "-114.094978074903",
        "quadrant": "SW",
        "url": {
            "url": "http://trafficcam.calgary.ca/loc111.jpg"
        }
    },
    {
        ":@computed_region_4a3i_ccfj": "2",
        ":@computed_region_4b54_tmc4": "7",
        ":@computed_region_kxmf_bzkv": "62",
        ":@computed_region_p8tp_5dkv": "7",
        "description": "Memorial Drive / 9 Street NW",
        "latitude": "51.0536259017801",
        "location": {
            "latitude": "51.0536259017801",
            "longitude": "-114.081180764729",
            "human_address": "{\"address\": \"\", \"city\": \"\", \"state\": \"\", \"zip\": \"\"}"
        },
        "longitude": "-114.081180764729",
        "quadrant": "NW",
        "url": {
            "url": "http://trafficcam.calgary.ca/loc59.jpg"
        }
    },

我正在尝试获取descriptionurl。 我获取description没问题,但是当我尝试获取url时,会抛出错误。

1 个答案:

答案 0 :(得分:1)

尝试将json_decode();true用作second参数,如果将true添加为第二个参数,则可以得到关联数组[array of array] else array of对象

$jsonToarray = json_decode($jsonData,true);// it will return array of array with assosiative format

并获得url

echo $jsonToarray['url']['url'];die;
  

您可以直接使用description,因为它不是嵌套的,但是url是嵌套的数组,因此它们之间是有区别的