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

2
  • 1
    You have an array with a single item, so loop through the properties for that object: for(var x in dataItem[0]) Commented Apr 9, 2014 at 18:39
  • possible duplicate of Access / process (nested) objects, arrays or JSON Commented Apr 9, 2014 at 18:47

2 Answers 2

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);
}
Sign up to request clarification or add additional context in comments.

4 Comments

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 ?
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.
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 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.
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

1 Comment

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: jsfiddle.net/B44LW/1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.