Solved

Iterate ove JSON object with arrays

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

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…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to count occurrences of each item in an array.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

733 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