ShanghaiD
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
I've tried:
Clearly I am not doing things right, but I have no idea what I should be doing.
I have assigned json data to a javascript variable (json1) using this code:
json1 = $.getJSON('selectqueries.cfc?method=getYesNoUncertain&returnformat=JSON');
Firefox shows me the json data was successfully got, in the following format:json1
{"COLUMNS":["V","D"],"DATA":[["1",true],["0",false],["?","Uncertain"]]}
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]);
all of which fail.Clearly I am not doing things right, but I have no idea what I should be doing.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
leakim971:
I've tried:
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",tr ue],["0",f alse],["?" ,"Uncertai n"]]}
What am I doing wrong?
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>
but I get a json1.COLUMNS is undefined error in Firebug although Firebug also shows the correct JSON Response of:
{"COLUMNS":["V","D"],"DATA
What am I doing wrong?
ASKER
gurvinder372:
When I try:
{"COLUMNS":["V","D"],"DATA ":[["1",tr ue],["0",f alse],["?" ,"Uncertai n"]]}
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>
I get x is not defined error in Firebug although Firebug also shows the correct JSON Response of:{"COLUMNS":["V","D"],"DATA
ASKER
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
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');
or
json1 = $.ajax({ "url":'selectqueries.cfc?method=getYesNoUncertain&returnformat=JSON', "async":false, dataType: 'json' }).responseText;
and I would really like to understand what to do fix that.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
leakim971:
Success! Thank you!
Success! Thank you!
You should read this :
http://www.phpmind.com/blog/2010/07/what-is-ajax-synchronous-and-asynchronous/
http://arshadinfo.wordpress.com/2008/05/25/what-is-the-difference-between-asynchronous-ajax-and-synchronous-request/
http://newtechies.blogspot.com/2007/12/synchronous-requests-versus.html
http://www.allinterview.com/showanswers/88917.html
http://www.phpmind.com/blog/2010/07/what-is-ajax-synchronous-and-asynchronous/
http://arshadinfo.wordpress.com/2008/05/25/what-is-the-difference-between-asynchronous-ajax-and-synchronous-request/
http://newtechies.blogspot.com/2007/12/synchronous-requests-versus.html
http://www.allinterview.com/showanswers/88917.html
ASKER
Thank you all for the collective insight/feedback which enabled me to get a better understanding of JSON and how to use it.
Open in new window
or :Open in new window
The following should work :
Open in new window