Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through JSON object List

I am returning a List<> from a webservice as a List of JSON objects. I am trying to use a for loop to iterate through the list and grab the values out of the properties. This is a sample of the returning JSON:

{"d":[{"__type":"FluentWeb.DTO.EmployeeOrder",  "EmployeeName":"Janet Leverling",  "EmployeeTitle":"Sales Representative",  "RequiredDate":"\/Date(839224800000)\/",  "OrderedProducts":null}]} 

So I am trying to extract the contents using something like this:

function PrintResults(result) {  for (var i = 0; i < result.length; i++) {      alert(result.employeename); } 

How should this be done?

like image 277
Nick Avatar asked Apr 29 '09 02:04

Nick


People also ask

How do I iterate through a list in JSON?

Use Object.values() or Object. entries(). These will return an array which we can then iterate over. Note that the const [key, value] = entry; syntax is an example of array destructuring that was introduced to the language in ES2015.

Can we loop through JSON object?

Looping Using JSON JSON stands for JavaScript Object Notation. It's a light format for storing and transferring data from one place to another. So in looping, it is one of the most commonly used techniques for transporting data that is the array format or in attribute values.

How do you import the JSON data and iterate through an array of objects?

1) Create a Maven project and add json dependency in POM. xml file. 2) Create a string of JSON data which we convert into JSON object to manipulate its data. 3) After that, we get the JSON Array from the JSON Object using getJSONArray() method and store it into a variable of type JSONArray.


1 Answers

Be careful, d is the list.

for (var i = 0; i < result.d.length; i++) {      alert(result.d[i].employeename); } 
like image 73
Burcu Dogan Avatar answered Oct 07 '22 19:10

Burcu Dogan