Solved

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

Posted on 2012-03-29
10
621 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
[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
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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
 

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

726 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