JSON.parse for an array of object

The server returns an array of the object in JSON. It looks like this:

{"d":"[
  {\"Id\":1,\"IsGood\":true,\"name1\":\"name1dsres\",\"Name2\":\"name2fdsfd\",\"name3\":  \"name3fdsgfd\",\"wasBorn\":\"\\/Date(284011000000)\\/\"},
  {\"Id\":2,\"IsGood\":false,\"name1\":\"fdsfds\",\"name2\":\"gfd3im543\",\"name3\":\"3543gfdgfd\",\"WasBorned\":\"\\/Date(281486800000)\\/\"}
]"}

I need to parse using the JSON.parse function. I do it like this:

   function myFunction(dataFromServer){
      var parsedJSON = JSON.parse(dataFromServer.d);
         for (var item in parsedJSON.d) {
          // how do I get the fields of current item?
      }

This code does not work, it returns undefined

for (var item in parsedJSON) {
      alert(item.Id);
}
+5
source share
3 answers

It works great

    function myFunction(dataFromServer){
       var parsedJSON = JSON.parse(dataFromServer.d);
       for (var i=0;i<parsedJSON.length;i++) {
            alert(parsedJSON[i].Id);
         }
 }

But this doens't

    function myFunction(dataFromServer){
           var parsedJSON = JSON.parse(dataFromServer.d);
           for (var item in parsedJSON) {
               alert(item.Id);
         }
 }
+6
source

You can just access them, like any object:

var id = item.Id;
if (item.IsGood) { ... }

If you want to list them somehow, check out this SO question .

+2
source

You can access them, like regular javascript objects, i.e. either item.idoritem['id']

0
source

All Articles