2013-04-23 39 views
-3

好吧我已經看了幾個例子,我似乎無法顯示數組或元素。它不斷返回爲空。我有以下的JSON(從Mapquest服務)json_decode無法顯示數組

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我真的只是試圖打印出經緯度和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(); 
} 
?> 
+0

這看起來不像JSON ... – elclanrs 2013-04-23 02:20:50

+1

這不是JSON。這是一個Javascript表達式。除了函數調用換行之外,鍵不加引號。這就是爲什麼普通的'json_decode'在這裏不起作用。 – mario 2013-04-23 02:21:00

+0

好的 - 任何建議 - 我有點新手在這裏,但任何指導將是偉大的 – 2013-04-23 02:22:29

回答

1

不包括'callback' => 'ResultSet'參數,才使得響應JSONP響應。

+0

謝謝 - 我知道它必須是簡單的東西 - – 2013-04-23 02:50:36