Link to home
Start Free TrialLog in
Avatar of ShanghaiD
ShanghaiDFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

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.
SOLUTION
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Avatar of ShanghaiD

ASKER

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?
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"]]}
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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
leakim971:

Success!  Thank you!
Thank you all for the collective insight/feedback which enabled me to get a better understanding of JSON and how to use it.