【问题标题】:having an issue parsing json in php在 php 中解析 json 时遇到问题
【发布时间】:2019-05-10 03:37:01
【问题描述】:

我对编码很陌生。我查看了网络上的许多示例,但似乎无法找到我要查找的内容。

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 时,它会引发错误。

【问题讨论】:

  • 嗨,你能告诉我们错误吗?
  • 请出示您的代码
  • 您需要添加一些代码和有问题的错误。寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定问题或错误必要的最短代码 重现它在问题本身。没有明确的问题陈述的问题对其他读者没有用处。见:How to create a Minimal, Complete, and Verifiable example.
  • @blainec:您可以看到 json 本身的差异.. 描述是直接元素,但如果您看到 URL 是嵌套的.. 最后,如果您尝试访问 $json['description'] 之类的描述那么 url 应该是 $json['url']['url']

标签: php json parsing


【解决方案1】:

尝试使用json_decode();true 作为second 参数,如果你添加true 作为第二个参数,那么你可以获得关联数组[数组数组] 否则数组对象

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

并获取url

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-06
    • 1970-01-01
    • 2014-02-24
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 2012-11-12
    • 1970-01-01
    相关资源
    最近更新 更多