簡體   English   中英

如何從AJAX成功函數解析這個json字符串?

[英]How can I parse this json string from an AJAX success function?

有人可以幫助解析json字符串中的一些數據。 這是JSON數據:

data = "{\"centerLatitude\":-41.22766,\"centerLongitude\":174.812761,\"mapTypeId\":\"google.maps.MapTypeId.ROADMAP\",\"zoom\":18}"

在我的AJAX代碼中,我有以下代碼:

success: function (mapDetailsData) {
    var data = jQuery.parseJSON(mapDetailsData);
    alert(data.centerLatitude);
    alert(data.centerLongitude);
}

我在控制台中收到以下錯誤:

未捕獲的SyntaxError:意外的令牌o

如果我指定JSON數據如下:

var data = jQuery.parseJSON('{\"centerLatitude\":-41.22766,\"centerLongitude\":174.812761,\"mapTypeId\":\"google.maps.MapTypeId.ROADMAP\",\"zoom\":18}');
alert(data.centerLatitude);
alert(data.centerLongitude);

alert顯示正確的數據。

如何需要編寫Ajax代碼以顯示正確的價值觀centerLatitudecenterLongitude

提前致謝。

假設您將datatype參數設置為json (或將其保留為默認設置並且它自己識別JSON格式),那么jQuery將自動為您反序列化響應。 您看到的錯誤通常表示您嘗試解析兩次。 嘗試這個:

success: function (mapDetailsData) {
    alert(mapDetailsData.centerLatitude);
    alert(mapDetailsData.centerLongitude);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM