Solved

Iterate ove JSON object with arrays

Posted on 2014-04-23
4
652 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
Comment Utility
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
Comment Utility
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
Comment Utility
var obj = jQuery.parseJSON(data);

else provide a link to your page
0
 

Author Closing Comment

by:kevbob650
Comment Utility
Helpful quick response!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

762 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

9 Experts available now in Live!

Get 1:1 Help Now