Solved

iterate over json returned by success function

Posted on 2012-03-19
9
427 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create an automated page index 9 53
Do alert on select 6 15
Responsive Font Size 6 31
Search Item in Table 2 23
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

820 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