Solved

iterate over json returned by success function

Posted on 2012-03-19
9
417 Views
Last Modified: 2012-03-19
first off the error i get is "Uncaught TypeError: Cannot read property 'length' of undefined"
                        
            i am using the following method to make a get request to a url
            now just for testing lets say vurl = "test.cfc?method=xyz&lnk=1&lnk2=abc"
            on success when i alert data i get: {"LNK2":"abc","_":1.332177382469E12,"LNK":1.0}
            
            so what am i doing wrong here?            
            
            $.ajax( {
                        type: "get",                        
                        url: vurl,                  
                        datatype: "json",
                        success: function(data) {
                              i've tried a variety of things here to loop through the json but get the error      
                        }                        
                  });

thanks ...
0
Comment
Question by:dgrafx
  • 4
  • 4
9 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37738826
try this way

for ( key in data )
{
   alert( date[ key ] );
}
0
 
LVL 25

Author Comment

by:dgrafx
ID: 37738850
thanks
the code you posted loops over each character as in:
t
h
e

c
a
t

i
n

etc
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37738879
first, tell me the output of

alert(data);

and then]

alert( typeof data );
0
 
LVL 25

Author Comment

by:dgrafx
ID: 37739048
data = {"LNK2":"abc","_":1.332177382469E12,"LNK":1.0}
typeof = string

so how do i convert to json? i imagine thats the issue ...
the url does have returnformat=json
thanks
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 40

Expert Comment

by:gurvinder372
ID: 37739068
got it!

before you run that for loop on this JSON, call this

data = eval (data);
0
 
LVL 25

Author Comment

by:dgrafx
ID: 37739078
i get Uncaught SyntaxError: Unexpected token :
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37739145
0
 
LVL 25

Author Comment

by:dgrafx
ID: 37739285
i ended up using:
data=JSON.parse(data);
$.each(data,function(ii,jj){ ..........

this works as expected (i believe)

is there code that says if the returned var is not an object to turn it into one?
thanks for your time
0
 
LVL 6

Accepted Solution

by:
jjperezaguinaga earned 500 total points
ID: 37739655
Greetings dgrafx,

First of all, eval is evil and should not be used in a real javascript application; there are many tricks that can allow you to have the same functionality without the security issues eval provides.

Second, alert is a bad way to debug an application. Use console.log instead, assuming you are using a modern browser it should be enough.

For instance:

success: function(data) {
 data=JSON.parse(data);
 console.log(data);
}       

Open in new window


If you already know the object and are $.each 'ing over it, then just do whatever you want for each element. For instance,

var o = [{name: 'jjperezaguinaga'}, {name: 'dgrafx'}, {name: 'john'}]

$.each(o, function(index, value) { // Use meaningful variable names ;)
    // $(this) can be used too...
    /* Your code here */
    console.log(value.name);
} );

Open in new window


For your question about the object, here's a nice trick to help you out

var name = data.name || {}

Open in new window


This means that if we have a name in our object data, assign it, other wise, create an empty object. This a common javascript pattern that you get to see around many libraries :)

Last but not least, EE has a Code tag to help review code. Use it! :)
Cheers!
-JJ
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

757 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