简体   繁体   中英

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 .

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:

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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