简体   繁体   English

如何遍历以下json列表?

[英]How do I loop through the following json list?

I am coding a jQuery ajax function that retrieves a list and I need to iterate through this list . 我正在编码一个检索列表的jQuery ajax函数,我需要遍历该list

This is the code I am using to serialize the list: 这是我用来序列化列表的代码:

JavaScriptSerializer().Serialize(listData);

Here is the json data after the data has been retrieved: 这是检索到数据后的json数据:

jsonData = "[{\"draggable\":false,\"icon\":null,\"latitude\":-41.22766,\"longitude\":174.812761,\"title\":\"LocationMarker1 74 Title\"},{\"draggable\":false,\"icon\":null,\"latitude\":-41.228029,\"longitude\":174.812926,\"title\":\"LocationMarker2 80 Title\"}]"

How do I iterate through this json list so that I can retrieve the individual values? 如何遍历此json列表,以便可以检索各个值?

I have tried the following: 我尝试了以下方法:

success: function (mapMarkerData) {
    for (var MapMarker in mapMarkerData) {
        alert(MapMarker["latitude"]);
        alert(MapMarker["longitude"]);
        alert(MapMarker["title"]);
        alert(MapMarker["draggable"]);
    }
}

For each alert, I am getting the following message: 对于每个警报,我都会收到以下消息:

undefined 未定义

Thanks in advance 提前致谢

var jsonData = "[{\"draggable\":false,\"icon\":null,\"latitude\":-41.22766,\"longitude\":174.812761,\"title\":\"LocationMarker1 74 Title\"},{\"draggable\":false,\"icon\":null,\"latitude\":-41.228029,\"longitude\":174.812926,\"title\":\"LocationMarker2 80 Title\"}]"


jsonData = JSON.parse(jsonData) //Parse it to make object

jsonData.forEach(function(val){
    alert(val.draggable);
    .
    .
    .
    alert(val.title);
})

Here is a possible solution. 这是一个可能的解决方案。

 var jsonData = "[{\\"draggable\\":false,\\"icon\\":null,\\"latitude\\":-41.22766,\\"longitude\\":174.812761,\\"title\\":\\"LocationMarker1 74 Title\\"},{\\"draggable\\":false,\\"icon\\":null,\\"latitude\\":-41.228029,\\"longitude\\":174.812926,\\"title\\":\\"LocationMarker2 80 Title\\"}]"; var obj = JSON.parse(jsonData); $.each(obj, function(i, row){ alert(row.draggable); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/json2/20140204/json2.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

var x = $.parseJSON(jsonData);
for(var o in x){
   console.log(x[o].latitude);
   console.log(x[o].longitude);
}

解决方案就像

var data = Json.Parse(jsonData); for (int i=0; i< data.length; i++){ alert(data[i]["latitude"]); }
success: function (mapMarkerData) {
    for (i in mapMarkerData) {
        alert(mapMarkerData[i].latitude);
        alert(mapMarkerData[i].longitude);
        alert(mapMarkerData[i].title);
        alert(mapMarkerData[i].draggable);
    }
}

you can use this code: 您可以使用以下代码:

var jsonData = "[{\"draggable\":false,\"icon\":null,\"latitude\":-41.22766,\"longitude\":174.812761,\"title\":\"LocationMarker1 74 Title\"},{\"draggable\":false,\"icon\":null,\"latitude\":-41.228029,\"longitude\":174.812926,\"title\":\"LocationMarker2 80 Title\"}]";

var jsonObj = JSON.parse(jsonData);
for(var index in jsonObj)
   alert(jsonObj[index].title);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM