Solved

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

Posted on 2012-03-29
10
614 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
 

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
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…

747 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

13 Experts available now in Live!

Get 1:1 Help Now