[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2012-03-29
10
Medium Priority
?
625 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:Gurvinder Pal Singh
Gurvinder Pal Singh earned 200 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 400 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 1400 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…

649 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