Solved

working with JSON data with multiple items returned in an array

Posted on 2013-11-12
9
252 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

863 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

20 Experts available now in Live!

Get 1:1 Help Now