我有一個PHP數組,我想傳遞給調用它的jQuery函數。然而,當我嘗試按照我在下面的方式檢索'lat'的值時,出現錯誤Cannot read property 'lat' of null
,顯然是因爲我不知道如何訪問多維JSON數組。任何人都可以告訴我如何?將多維JSON數組傳遞給jQuery函數
PHP陣列
Array
(
[0] => Array
(
[price] => 1600
[bedroom] => 1
[lat] => -71.119385
[lng] => 42.373917
[distance] => 6.65195429565453
)
[1] => Array
(
[price] => 1800
[bedroom] => 1
[lat] => -71.104248
[lng] => 42.368172
[distance] => 4.957829810472103
)
}
這被編碼成
JSON
[{"price":"1600","bedroom":"1","lat":"-71.119385","lng":"42.373917","distance":"6.65195429565453"},{"price":"1800","bedroom":"1","lat":"-71.104248","lng":"42.368172","distance":"4.957829810472103"}]
的jQuery
$(function() {
$("#search_button").click(function(e){
e.preventDefault();
var search_location = $("#search_location").val();
$.getJSON('index.php/main/get_places', {search_location: search_location}, function(json){
$("#result").html(json.lat);
console.log(json.lat);
});
});
});
'的console.log(JSON [0] .lat);'沒有造成什麼在JavaScript控制檯登錄... – Nyxynyx
那麼'JSON'是不是有什麼你說它是:) – Hamish
我檢查了不使用AJAX返回的JSON字符串。這真的是JSON哈哈......這是否與'JSON'字符串沒有任何索引編號像'1 :, 2:' – Nyxynyx