无法将JSON对象转换为任何可用的对象

时间:2013-10-29 21:54:37

标签: javascript php ajax json

我对JSON还不太好,所以请原谅这是一个重大的菜鸟错误。我正在向本地文件发送一个查询,该文件对外部站点的API执行cURL并返回一个JSON对象。因为我必须为API支付x查询,所以我只是复制并粘贴了一个,我将其用于替换cURL。我有以下脚本:

$.ajax({
             type: 'GET',
             url: 'ajax.php?v='+value, //with this being an input value, which is totally irrelevant because I'm not actually doing the cURL query anyway
             dataType: 'json',
             success:function(json){

                 var o_response = json;
                 json = $.parseJSON(json);

                 alert(o_response.toSource());
                 alert(json.toSource());

             },
             error: function (xhr, ajaxOptions, thrownError) {
                alert('There appears to be a problem with the information you submitted. Please try again or contact us.');
              }
        });

ajax.php中的PHP看起来像这样:

<?

if (isset($_GET['v']) && $_GET['v'] != '') {

$response = '[{"query":"14-22-25-02-W5","response":{"status":"ok","err":[],"lat":51.152259,"lng":-114.202199,"country":"Canada","province":"AB","city":"Calgary","street":"49 Royal Vista Drive NW","street_prox":78,"address":"49 Royal Vista Drive NW, Calgary, AB","lsd":"14-22-25-2 W5","lsd_border":[[51.150459,-114.199327],[51.150447,-114.205067],[51.154059,-114.205071],[51.154072,-114.199332],[51.150459,-114.199327]],"uwi":"","nts":"","nts_border":[],"utm":"11S 695661E 15670479N","utm_v":"Zone 11, 695661 meters easting, 15670479 meters northing (Southern Hemisphere)"}}]';

echo json_encode($response);

}


?>

$ response与API给我的完全相同。

我想要做的是从中获取“lat”和“lng”值。我的JavaScript文件中的第一个示例,“alert(o_response.toSource());” bit使它成为一个字符串,这很好,但我想要一个对象。第二个例子“alert(json.toSource());”使其成为一个对象,但删除所有键周围的引号。例如,它执行此操作:

[{query:"14-22-25-02-W5", response:{status:"ok", err:[], lat:51.152259, lng:-114.202199, country:"Canada", province:"AB", city:"Calgary", street:"49 Royal Vista Drive NW", street_prox:78, address:"49 Royal Vista Drive NW, Calgary, AB", lsd:"14-22-25-2 W5", lsd_border:[[51.150459, -114.199327], [51.150447, -114.205067], [51.154059, -114.205071], [51.154072, -114.199332], [51.150459, -114.199327]], uwi:"", nts:"", nts_border:[], utm:"11S 695661E 15670479N", utm_v:"Zone 11, 695661 meters easting, 15670479 meters northing (Southern Hemisphere)"}}]

请注意“查询”,“响应”,“状态”,“lat”,“lng”等不再有引号。我想这就是它应该工作的方式。那么,如果我尝试通过执行以下操作来获得“响应”:

alert(json.response);
alert(json['response']);
alert(json[1]);

我得到的是3个未定义的警报。

我显然错过了一些东西。它的格式不正确吗?我解析或编码我不应该做的事情吗?

任何帮助都会非常感激。

谢谢。

1 个答案:

答案 0 :(得分:3)

您的响应字符串已经是JSON,因此无需通过json_encode运行它。只需使用以下

<?php
header('Content-type: application/json');

if (empty($_GET['v'])) {
    http_response_code(400);
    echo json_encode(['error' => 'Missing "v" parameter']);
    exit;
}

echo '[{"query":"14-22-25-02-W5","response":{"status":"ok","err":[],"lat":51.152259,"lng":-114.202199,"country":"Canada","province":"AB","city":"Calgary","street":"49 Royal Vista Drive NW","street_prox":78,"address":"49 Royal Vista Drive NW, Calgary, AB","lsd":"14-22-25-2 W5","lsd_border":[[51.150459,-114.199327],[51.150447,-114.205067],[51.154059,-114.205071],[51.154072,-114.199332],[51.150459,-114.199327]],"uwi":"","nts":"","nts_border":[],"utm":"11S 695661E 15670479N","utm_v":"Zone 11, 695661 meters easting, 15670479 meters northing (Southern Hemisphere)"}}]';

在JS方面,jQuery已经知道响应有效负载是JSON,所以不需要通过json运行$.parseJSON。您可以直接访问对象文字属性,例如

json[0].response.lat