Solved

Iterate ove JSON object with arrays

Posted on 2014-04-23
4
657 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now