Solved

jQuery getJSON -- how do I get at the data?

Posted on 2012-03-29
10
616 Views
Last Modified: 2012-03-29
This is probably very simple for the experts -- but not for me (still trying to understand JSON):
I have assigned json data to a javascript variable (json1) using this code:
json1 = $.getJSON('selectqueries.cfc?method=getYesNoUncertain&returnformat=JSON');

Open in new window

Firefox shows me the json data was successfully got, in the following format:
json1
{"COLUMNS":["V","D"],"DATA":[["1",true],["0",false],["?","Uncertain"]]}

Open in new window

Where I am stuck is -- how do I use the data (in javascript)?
I've tried:
alert(json1.COLUMNS[1]);
alert(json1.COLUMNS[D][1]);
alert(json1.COLUMNS[1][1]);
alert(json1.DATA[1]);
alert(json1.DATA[D][1]);
alert(json1.DATA[1][1]);
alert(json1[1]);
alert(json1[1][1]);

Open in new window

all of which fail.
Clearly I am not doing things right, but I have no idea what I should be doing.
0
Comment
Question by:ShanghaiD
10 Comments
 
LVL 40

Assisted Solution

by:gurvinder372
gurvinder372 earned 50 total points
ID: 37781117
eval it first
json1 = eval (json1);

while ( x in json1 )
{
   alert(json[x]);
}
0
 
LVL 17

Assisted Solution

by:sonawanekiran
sonawanekiran earned 100 total points
ID: 37781135
0
 
LVL 82

Expert Comment

by:leakim971
ID: 37781532
Use :
json1 = $.ajax({ "url":'selectqueries.cfc?method=getYesNoUncertain&returnformat=JSON', "async":false, dataType: 'json' }).responseText;

Open in new window

or :
$.ajaxSetup({async:false});
json1 = $.getJSON('selectqueries.cfc?method=getYesNoUncertain&returnformat=JSON');

Open in new window


The following should work :
alert(json1.COLUMNS[0]);
//alert(json1.COLUMNS[0][1]); // No
//alert(json1.COLUMNS[1][1]); // No
alert(json1.DATA[1]); // Not good
alert(json1.DATA[0][1]);
alert(json1.DATA[1][1]);
//alert(json1[1]); // No
//alert(json1[1][1]); // No

Open in new window

0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:ShanghaiD
ID: 37785339
leakim971:

I've tried:

<script src="../js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
json1 = $.ajax({ "url":'selectqueries.cfc?method=getYesNoUncertain&returnformat=JSON', "async":false, dataType: 'json' }).responseText;
alert(json1.COLUMNS[0]);
alert(json1.DATA[0][1]);
alert(json1.DATA[1][1]);
});	
</script> 

Open in new window


but I get a json1.COLUMNS is undefined error in Firebug although Firebug also shows the correct JSON Response of:
{"COLUMNS":["V","D"],"DATA":[["1",true],["0",false],["?","Uncertain"]]}

What am I doing wrong?
0
 

Author Comment

by:ShanghaiD
ID: 37785354
gurvinder372:

When I try:

<script src="../js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
json1 = $.getJSON('selectqueries.cfc?method=getYesNoUncertain&returnformat=JSON');
json1 = eval(json1);
while (x in json1)
{
alert(json[x]);
}
});	
</script> 

Open in new window

I get x is not defined error in Firebug although Firebug also shows the correct JSON Response of:
{"COLUMNS":["V","D"],"DATA":[["1",true],["0",false],["?","Uncertain"]]}
0
 

Author Comment

by:ShanghaiD
ID: 37785379
sonawanekiran:

Your code works perfectly (and helps me understand how to reference the returned JSON elements).  Thanks!

However, I still cannot get success with my actual cfc call, either with
json1 = $.getJSON('selectqueries.cfc?method=getYesNoUncertain&returnformat=JSON');

Open in new window

or
json1 = $.ajax({ "url":'selectqueries.cfc?method=getYesNoUncertain&returnformat=JSON', "async":false, dataType: 'json' }).responseText;

Open in new window

and I would really like to understand what to do fix that.
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 350 total points
ID: 37785418
what about :

<script src="../js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
json1 = eval( "(" + $.ajax({ "url":'selectqueries.cfc?method=getYesNoUncertain&returnformat=JSON', "async":false, dataType: 'json' }).responseText + ")" );
alert(json1.COLUMNS[0]);
alert(json1.DATA[0][1]);
alert(json1.DATA[1][1]);
});	
</script> 

Open in new window

0
 

Author Comment

by:ShanghaiD
ID: 37785429
leakim971:

Success!  Thank you!
0
 
LVL 82

Expert Comment

by:leakim971
ID: 37785434
0
 

Author Closing Comment

by:ShanghaiD
ID: 37785440
Thank you all for the collective insight/feedback which enabled me to get a better understanding of JSON and how to use it.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
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…

816 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

8 Experts available now in Live!

Get 1:1 Help Now