3

I would like to parse some JSON data but am not sure what I am doing wrong.

The code I am using is as follows:

$.getJSON('bd/getuserDetails',userID,function(data) {
    $.each(data.UserDTO,function(index, value){
         alert(value);
    });
});

In my Java action class I am populating UserDTO with relevant details like name, age, etc.

The above code is able to parse my object, but my intentions are to access the value based on the name like alert(value.name); //user name. Currently it's parsing and displaying the values but I am not able to determine the name for the given value.

How can I access the collection with the name for the corresponding value?

1
  • can you add information that you receive in data variable? before $.each statement? Commented Aug 16, 2012 at 17:05

1 Answer 1

6
$.getJSON('bd/getuserDetails',userID,function(data) {
    $.each(data.UserDTO,function(key, value){
         alert(key + ' : ' + value);
    });
});

Suppose if your data.UserDTO is like:

data.UserDTO = {'name' : 'one', 'title' : 'Mr', ..};

then in above loop:

key => name, title...

value => one, Mr...

But

if your

data.UserDTO = [ {'name' : 'one', 'title' : 'Mr', ..}, 
                 {'name' : 'two', 'title' : 'Mrs', ..} 
               ];

then above loop:

key => 0,1... (index of each object within that array)

value => {'name' : 'one', 'title' : 'Mr', ..}, {'name' : 'two', 'title' : 'Mrs', ..} ... 
Sign up to request clarification or add additional context in comments.

1 Comment

that's perfect will do my job to maximum.Thanks i was missing this key part ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.