Solved

Iterate ove JSON object with arrays

Posted on 2014-04-23
4
665 Views
Last Modified: 2014-04-24
I have a perl sub producing  a json object that is being returned from an ajax call.

success: function (data) {
Resulting object looks like this  ( if I view it using var obj = data.toSource() ) :

{"on_call_list":[{"developerID":"dev1234","developerName":"Name","developerPhone":"444-444-4444"},{"developerID":"dev2345","developerName":"Name2","developerPhone":"555-555-5555"}]};

So, you'll note, the first object value is an array, but the object itself is not in an array.
Not able to itarate over the object to get the values.
Jquery approach would be best since I'm in an ajax call return.
Thanks in advance for the assist!

Tried a number of approaches but thought the best way would be:
Assign a variable for the object...
var obj = {"on_call_list":[{...................}];

Then try to loop thru the array, which is the value of the first item...
$.each(obj.on_call_list, function(key,value) {
     console.log(key + "=" + value);
});

Getting an error from jquery, "e is undefined" ??
0
Comment
Question by:kevbob650
  • 2
  • 2
4 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 40018930
Test page : http://jsfiddle.net/CC448/ 
var obj = {"on_call_list":[{"developerID":"dev1234","developerName":"Name","developerPhone":"444-444-4444"},{"developerID":"dev2345","developerName":"Name2","developerPhone":"555-555-5555"}]};

var arr = obj.on_call_list;

for(var i=0;i<arr.length;i++) {
      var jsonObj = arr[i];
      for(var key in jsonObj) {
         value = jsonObj[key];
         console.log(key + "=" + value);
         alert(key + "=" + value);
      }
}

Open in new window

0
 

Author Comment

by:kevbob650
ID: 40018951
That looks good, but i'm getting an error "arr is not defined" because obj.on_call_list  is not recognized.  Could the problem be that using var obj = data.toSource(); is incorrect ?   Is the a better way to get the object out of the "data" returned from the ajax call?
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40018957
var obj = jQuery.parseJSON(data);

else provide a link to your page
0
 

Author Closing Comment

by:kevbob650
ID: 40021311
Helpful quick response!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question