• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

working with JSON data with multiple items returned in an array

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
NickMalloy
Asked:
NickMalloy
  • 4
  • 3
  • 2
1 Solution
 
leakim971PluritechnicianCommented:
could you post this JSON?
0
 
Chris StanyonCommented:
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
 
NickMalloySystems LeadAuthor Commented:
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
Chris StanyonCommented:
We need to see the generated code that you send to your page - not the code that generates it.
0
 
leakim971PluritechnicianCommented:
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
 
NickMalloySystems LeadAuthor Commented:
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
 
leakim971PluritechnicianCommented:
add this to get the JSON in string format :

var strJSON = JSON.stringify( msg );
0
 
NickMalloySystems LeadAuthor Commented:
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
 
leakim971PluritechnicianCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now