[英]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.