Solved

iterate over json returned by success function

Posted on 2012-03-19
9
428 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
[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
  • 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
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 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Aspx calendar pop up control 3 38
Read text on Table 7 49
javascript duplicates 9 25
Textbox autocomplete using jquery in asp.net 13 49
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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

738 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