如何从AJAX成功函数解析这个json字符串?

时间:2015-05-26 09:06:35

标签: javascript jquery ajax json json-deserialization

有人可以帮助解析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的正确值?

提前致谢。

1 个答案:

答案 0 :(得分:3)

假设您将showme = function(index){ //How to animate a particular marker by an index? } 参数设置为datatype(或将其保留为默认设置并且它自己识别JSON格式),那么jQuery将自动为您反序列化响应。您看到的错误通常表示您尝试解析两次。试试这个:

json