Solved

Iterate ove JSON object with arrays

Posted on 2014-04-23
4
672 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The viewer will learn how to dynamically set the form action using jQuery.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

724 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