[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

working with JSON data with multiple items returned in an array

Posted on 2013-11-12
9
Medium Priority
?
259 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 44

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 44

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
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…
Suggested Courses

656 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