2016-03-28 27 views
0

我正在使用php並使用json_encode()函數我返回了/回顯的JSON數組。我使用jQuery的Ajax和成功地檢索此數據:使用JSON.stringify並且無法訪問JSON數組

[{"id":"1132","city":"Manila Central Post Office","province":"Manila"}] 

我用順便說一句我的Javascript驗證碼:

var val = jQuery.parseJSON(JSON.stringify(result)); 

,當我試圖訪問像陣列上的數據:

console.info(val.city); // results to undefine 

它給了我'未定義'的結果。我試過在for循環中仍然不起作用。也許我做錯了什麼,任何幫助都會很棒。致謝

Ajax代碼:

$.ajax({ 
    type: 'POST', 
    url: path, 
    cache: false, 
    data: postData, 
    success: function (result) { 
     var val = jQuery.parseJSON(JSON.stringify(result, false, replacer)); 
     var val2 = jQuery.parseJSON(val); 
     console.info(val2); 
     console.info(val2.id); 
    }, 
    error: function (e) { 
     $("#sys_msg").html(e); 
     $("#myModal").modal({ 
      show: true, 
      backdrop: false, 
      keyboard: true 
     }); 
    } 
}); 
+0

'val'是一個數組,具有'city'鍵的對象是該數組中的第一項。無論如何,爲什麼你需要將其重新解析呢? – jonrsharpe

+0

你可以在上面的問題中添加ajax代碼嗎? – Jai

+0

var val = jQuery.parseJSON(JSON.stringify(result,false,replacer)); var val2 = jQuery.parseJSON(val); –

回答

1

val是一個數組。你需要像下面那樣指定索引。

var result = [{ "id": "1132", "city": "Manila Central Post Office", "province": "Manila" }]; 
 
var val = jQuery.parseJSON(JSON.stringify(result)); 
 
alert(val[0].city);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

+0

[對象{id =「1132」,city =「馬尼拉中央郵局」,省=「馬尼拉」}]。它又變回了對象。 –

+0

我已經投票了,因爲你能夠提取JSON數據。謝謝:) –

+0

其實你不需要使用'var val = jQuery.parseJSON(JSON.stringify(result));'line。你可以直接使用'result [0] .city'。 @JomarLlevado – Azim

1

這裏val是對象的數組,這樣你就可以不通過調用val.city直接訪問city值。如果返回的數據已使用json_encode()進行編碼,則只需使用$.parseJSON(data)即可。下面的代碼片段展示瞭如何做到這 -

var temp = '[{"id":"1132","city": "Manila Central Post Office", "province":"Manila"},{"id":"1133","city": "Another Test Post Office", "province":"Test"}]'; //Defined temp as string since the data response returned from the server should also be a json encoded string. 
 

 
var val = $.parseJSON(temp); 
 

 
$.each(val, function(index, item) { 
 
    var city = item.city; 
 
    $('.container').append('City: '+city+'<br/>'); 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
 
<div class="container"></div>

+0

不知何故,這解決了一些問題。謝謝 –

+0

@JomarLlevado你還面臨什麼其他問題? –

+0

我想拉數據,這是你給我,我試圖把它放在一個指定的HTML DOM,如輸入 –

0

見,這個問題可以很容易地通過dataType:"json"解決。如果你沒有在你的ajax代碼中附加這個,那麼返回的響應將是一個json字符串,在這種情況下,只有你必須用jQuery.parseJSON()JSON.parse()解析它。

因此,您可以將dataType添加到ajax,或者只是將其解析。

success: function(result){ 
    var val = jQuery.parseJSON(result); // or JSON.parse(result); 
    console.info(val[0].city); 
}, 

隨着dataType

$.ajax({ 
    type: 'POST', 
    url: path, 
    cache: false, 
    data: postData, 
    dataType: 'json', //<-----add this here 
    success: function(result) { 
    for (o in result) { // <----and just use this way 
     console.info(o.city); 
    } 
    }, 
    error: function(e) { 
    $("#sys_msg").html(e); 
    $("#myModal").modal({ 
     show: true, 
     backdrop: false, 
     keyboard: true 
    }); 
    } 
}); 
0

您不必stringify它隨後又對解析JSON。您可以在您的AJAX中添加dataType: "json"

$.ajax({ 
    type: 'POST', 
    url: path, 
    cache: false, 
    data: postData, 
    dataType: "json", 

    success: function (result) { 

     console.info(result); 
     console.info(result.id); 
    }, 
    error: function (e) { 
     $("#sys_msg").html(JSON.stringify(e)); 
     $("#myModal").modal({ 
      show: true, 
      backdrop: false, 
      keyboard: true 
     }); 
    } 
});