Solved

working with JSON data with multiple items returned in an array

Posted on 2013-11-12
9
253 Views
Last Modified: 2013-11-13
I have a Json response from an ajax call that I keep getting an undefined error when trying to loop through a section.
I'm returning multiple items in a response and want to start at the 3rd {....}. My problem is I loop too many times and error out the loop with an undefined error. I've tried var === 'undefined', but that doesn't seem to work.  This is what I have, How do I prevent it from going past the array boundary.

for (var i = 2 in msg[i].Job) {
                    
                         $('#customFields').append('...');
                     
                 }

Open in new window

0
Comment
Question by:NickMalloy
  • 4
  • 3
  • 2
9 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 39643172
could you post this JSON?
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39643308
If your data's coming in as an array, then use the each() function to loop through it:

$.each(data, function(index, element) {
     console.log(element);
});

Open in new window

As leakim said though - we'd need to see the JSON response to your AJAX request
0
 

Author Comment

by:NickMalloy
ID: 39645093
This is what I send to the page. The loop is what I am interested in. I have no idea how many could be there.

                     
   string JSON = "[";

                        JSON = JSON + "{";
                        JSON = JSON + "\"JobD\":";
                        JSON = JSON + "\"" + DD.Job + "\",";
                        JSON = JSON + "\"Description\":";
                        JSON = JSON + "\"" + DD.Description + "\",";
                        JSON = JSON + "\"Level\":";
                        JSON = JSON + "\"" + DD.Level + "\",";
                        JSON = JSON + "\"Dept\":";
                        JSON = JSON + "\"" + DD.Dept + "\",";
                        JSON = JSON + "\"Range\":";
                        JSON = JSON + "\"" + RangeSpread + "%\"";
                        JSON = JSON + "},";
                        JSON = JSON + "{";
                        JSON = JSON + "\"DeptHigh\":";
                        JSON = JSON + "\"" + DeptHigh + "\",";
                        JSON = JSON + "\"DeptLow\":";
                        JSON = JSON + "\"" + DeptLow + "\",";
                        JSON = JSON + "\"DeptAvg\":";
                        JSON = JSON + "\"" + DeptAvg + "\",";
                        JSON = JSON + "\"EntHigh\":";
                        JSON = JSON + "\"" + EntHigh + "\",";
                        JSON = JSON + "\"EntLow\":";
                        JSON = JSON + "\"" + EntLow + "\",";
                        JSON = JSON + "\"EntAvg\":";
                        JSON = JSON + "\"" + EntAvg + "\",";
                        JSON = JSON + "\"DeptCount\":";
                        JSON = JSON + "\"" + Deptcount + "\",";
                        JSON = JSON + "\"EntCount\":";
                        JSON = JSON + "\"" + Entcount + "\"";
                        JSON = JSON + "}";

                        foreach (JobInformation JS in JList)
                        {

                                JSON = JSON + ",";
                                JSON = JSON + "{";
                                JSON = JSON + "\"Job\":";
                                JSON = JSON + "\"" + firstElement.DESCRIPTION + "\",";
                                JSON = JSON + "\"DeptGLowArray\":";
                                JSON = JSON + "\"" + DeptEntLow + "\",";
                                JSON = JSON + "\"DeptGHighArray\":";
                                JSON = JSON + "\"" + DeptEntHigh + "\",";
                                JSON = JSON + "\"DeptGAvgArray\":";
                                JSON = JSON + "\"" + DeptEntAvg + "\",";
                                JSON = JSON + "\"DeptGCountArray\":";
                                JSON = JSON + "\"" + DeptEntCount + "\"";
                                JSON = JSON + "}";
                        }

                        JSON = JSON + "]";

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39645118
We need to see the generated code that you send to your page - not the code that generates it.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39645122
you post the code generating your json
we would like to see your json itself, the generated json itself

At first look, from what you post, your json is not valid :

{";
                                JSON = JSON + "\"Job\":";
                                JSON = JSON + "\"" + firstElement.DESCRIPTION + "\",";
                                JSON = JSON + "\"DeptGLowArray\":";
                                JSON = JSON + "\"" + DeptEntLow + "\",";
                                JSON = JSON + "\"DeptGHighArray\":";
                                JSON = JSON + "\"" + DeptEntHigh + "\",";
                                JSON = JSON + "\"DeptGAvgArray\":";
                                JSON = JSON + "\"" + DeptEntAvg + "\",";
                                JSON = JSON + "\"DeptGCountArray\":";
                                JSON = JSON + "\"" + DeptEntCount + "\"";
                                JSON = JSON + "}

Open in new window

0
 

Author Comment

by:NickMalloy
ID: 39645157
what's the best way to get the full response info? I'm using Visual Studio in debug mode. I hover over my return but it's just showing.

[[object Object],[object Object],[object Object],[object Object],[object Object]]

I can expand those, but doesn't show the clean response.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39645198
add this to get the JSON in string format :

var strJSON = JSON.stringify( msg );
0
 

Author Comment

by:NickMalloy
ID: 39645242
x's replace the returned data as it is sensitive.

[{\"JobD\":\"xxxxx\",\"Description\":\"xxxxxxxx\",\"ProcessLevel\":\"xxx\",\"Dept\":\"xxxx\",\"Range\":\"xxxx\"},{\"DeptHigh\":\"xxxx\",\"DeptLow\":\"xxxx\",\"DeptAvg\":\"xxxx\",\"EntHigh\":\"xxxx\",\"EntLow\":\"xxxx\",\"EntAvg\":\"xxxx\",\"DeptCount\":\"xx\",\"EntCount\":\"xx\"},{\"Job\":\"xxxxxxx\",\"DeptGLowArray\":\"xxxx\",\"DeptGHighArray\":\"xxxx\",\"DeptGAvgArray\":\"xxxxx\",\"DeptGCountArray\":\"x\"},{\"Job\":\"xxxxxxx\",\"DeptGLowArray\":\"xxxx\",\"DeptGHighArray\":\"xxxx\",\"DeptGAvgArray\":\"xxxxx\",\"DeptGCountArray\":\"x\"},{\"Job\":\"xxxxxxx\",\"DeptGLowArray\":\"xxxx\",\"DeptGHighArray\":\"xxxx\",\"DeptGAvgArray\":\"xxxxx\",\"DeptGCountArray\":\"x\"}]

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 300 total points
ID: 39645273
Check how it look :
msg
for(var i=2;i<msg.length;i++) {
      var Job = msg[i].Job;
      var DeptGLowArray = msg[i].DeptGLowArray;
      // other field
      $('#customFields').append('...');
}

Open in new window


or :

for(var i=0;i<msg.length;i++) {
   if(msg[i].Job) { // Do we've a Job field inside?
      var Job = msg[i].Job;
      var DeptGLowArray = msg[i].DeptGLowArray;
      // other field
      $('#customFields').append('...');
   }
}

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 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