?
Solved

working with JSON data with multiple items returned in an array

Posted on 2013-11-12
9
Medium Priority
?
257 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
[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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1200 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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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)
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…
Suggested Courses
Course of the Month12 days, 21 hours left to enroll

777 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