0

If I have a JSON Object Map :

var dataItem=[{
  "Lucy":{
    "id": 456,
    "full_name": "GOOBER, ANGELA",
    "user_id": "2733245678",
    "stin": "2733212346"
  },
  "Myra":{
    "id": 123,
    "full_name": "BOB, STEVE",
    "user_id": "abc213",
    "stin": "9040923411"
  }
}]

I want to iterate through this list and access the names (i.e. Lucy, Myra ) and corresponding information

All the loops that I came across looped through the list like this :

var dataItem = [
    {"Name":"Nthal","Class":3,"SubjectName":"English "},
    {"Name":"Mishal","Class":4,"SubjectName":"Grammer"},
    {"Name":"Sanjeev","Class":3,"SubjectName":"Social"},
    {"Name":"Michal","Class":5,"SubjectName":"Gk"},
]

for(x in dataItem)
{
alert(dataItem[x].Name);
alert(dataItem[x].Class);
alert(dataItem[x].SubjectName);
}

Thanks in advance

greengrassbluesky
  • 373
  • 1
  • 6
  • 23
  • 1
    You have an array with a single item, so loop through the properties for that object: `for(var x in dataItem[0])` – Matt Burland Apr 09 '14 at 18:39
  • possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Felix Kling Apr 09 '14 at 18:47

2 Answers2

1

What you have there is not JSON, maybe because you've already parsed it. You have is an array consisting of a single object, with names for its keys. Regardless, I'll show you how to access that data:

var data = dataItem[0];
for(name in data) {
    alert(name);
    alert(data[name].id);
    alert(data[name].full_name);
}
Paul
  • 139,544
  • 27
  • 275
  • 264
  • Thanks very much for a quick reply. I added this code and for name I get each and every letter not the whole name. so in case of "Lucy" I get "L", "U" ,"C", "Y" for each iteration of for and not the whole name "lucy". Can you please help me ? Where am I going wrong ? – greengrassbluesky Apr 09 '14 at 20:04
  • It seems to me like its never converting my JSON string to JSON Object. I tried using JSON.parse and $.parseJSON. None of them seem to work. – greengrassbluesky Apr 09 '14 at 23:01
  • sorry for the inconvinience.. my problem was that I was logging using console.log (" jsonObj:"+jsonObj); which was converting my object to string and hence printing wrong data. – greengrassbluesky Apr 10 '14 at 18:44
  • @greengrassbluesky No problem, if you want to log an object with a string like that, you can pass them as separate arguments to `console.log`, like this: `console.log('jsonObj:', jsonObj);`. That should give you some nice output. – Paul Apr 10 '14 at 19:49
0
for (var x in dataItem[0]) {
    if (dataItem[0].hasOwnProperty(x)) {
        console.log(x);
    }
}

http://jsfiddle.net/B44LW/

If you want other properties, then you can use the bracket notation:

dataItem[0][x].id
Matt Burland
  • 44,552
  • 18
  • 99
  • 171
  • Thanks a lot for creating a fiddle. I am sorry that I didnt mention the in the original question that I am also converting JSON string to JSON object before looping through it. turns out the problem is in conversion. I updated the fiddle to following: http://jsfiddle.net/B44LW/1/ – greengrassbluesky Apr 09 '14 at 23:46