Solved

iterate over json returned by success function

Posted on 2012-03-19
9
426 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

786 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