[英]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代碼以顯示正確的價值觀centerLatitude
和centerLongitude
?
提前致謝。
假設您將datatype
參數設置為json
(或將其保留為默認設置並且它自己識別JSON格式),那么jQuery將自動為您反序列化響應。 您看到的錯誤通常表示您嘗試解析兩次。 嘗試這個:
success: function (mapDetailsData) {
alert(mapDetailsData.centerLatitude);
alert(mapDetailsData.centerLongitude);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.