【问题标题】:json_decode can't display array [closed]json_decode 无法显示数组 [关闭]
【发布时间】:2013-04-23 02:19:19
【问题描述】:

好的,我查看了一些示例,但似乎无法显示数组或元素。它一直以 null- 的形式返回。我有以下 JSON(来自 mapquest)

renderGeocode({
    results: [
        {
            locations: [
                {
                    latLng: {
                        lng: -90.1978,
                        lat: 38.627201
                    },
                    adminArea4: "Saint Louis City",
                    adminArea5Type: "City",
                    adminArea4Type: "County",
                    adminArea5: "Saint Louis",
                    street: "",
                    adminArea1: "US",
                    adminArea3: "MO",
                    type: "s",
                    displayLatLng: {
                        lng: -90.1978,
                        lat: 38.627201
                    },
                    linkId: 0,
                    postalCode: "",
                    sideOfStreet: "N",
                    dragPoint: false,
                    adminArea1Type: "Country",
                    geocodeQuality: "CITY",
                    geocodeQualityCode: "A5XAX",
                    mapUrl: "http://www.mapquestapi.com/staticmap/v4/getmap?key=123456789&type=map&size=225,160&pois=purple-1,38.627201,-90.1978,0,0|&center=38.627201,-90.1978&zoom=12&rand=1390479880",
                    adminArea3Type: "State"
                }
            ],
            providedLocation: {
                location: "SAint Louis,mo"
            }
        }
    ],
    options: {
        ignoreLatLngInput: false,
        maxResults: -1,
        thumbMaps: true
    },
    info: {
        copyright: {
            text: "© 2013 MapQuest, Inc.",
            imageUrl: "http://api.mqcdn.com/res/mqlogo.gif",
            imageAltText: "© 2013 MapQuest, Inc."
        },
        statuscode: 0,
        messages: [

        ]
    }
})

我试图将它添加到这样的数组中

$array =json_decode($data, true);

但是,无论我如何尝试打印或检查内容,它都会返回为 null,我实际上只是想打印出 lat lng 和 adminArea5。 任何帮助都会很棒。

这是完整的代码

<?php
$where = filter_input(INPUT_GET, 'where', FILTER_SANITIZE_STRING);
$source = "getLat";
if ($source =="getLat")
{
    ob_start();
    $getsource = array('location' =>$where,
            'type'=>$getWhat,
            'callback'=>'ResultSet'
    );
    $url = "http://www.mapquestapi.com/geocoding/v1/address?key=123456789" . http_build_query($getsource, '', "&");
//print_r($url);
    $data_mapquest = file_get_contents($url);
    $array = json_decode($data_mapquest, true);
    ob_end_flush();
}
?>

【问题讨论】:

  • 这看起来不像 JSON...
  • 这不是 JSON。这是一个 Javascript 表达式。除了函数调用包装之外,键是不加引号的。这就是为什么普通的json_decode 在这里不起作用的原因。
  • 好的 - 任何建议 - 我在这里有点新手,但任何指导都会很棒
  • 请在您的代码中添加适当的制表符。
  • OK - 我想我可以剪掉 renderGeocode

标签: php arrays json


【解决方案1】:

不要包含'callback' =&gt; 'ResultSet' 参数,这会使响应成为JSONP 响应。

【讨论】:

  • 谢谢——我知道这一定很简单——
猜你喜欢
  • 2018-10-02
  • 1970-01-01
  • 2022-06-30
  • 2013-05-22
  • 1970-01-01
  • 2019-01-25
  • 2015-09-16
  • 1970-01-01
  • 2021-07-20
相关资源
最近更新 更多