wood burning stoves 2.0*
The moose likes HTML, CSS and JavaScript and the fly likes Unable to extract JSON Response in JQuery function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Unable to extract JSON Response in JQuery function" Watch "Unable to extract JSON Response in JQuery function" New topic
Author

Unable to extract JSON Response in JQuery function

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Hi ,

This is my JSON Response inside my servlet shown from Firebug .


I am unable to access this data inside my jquery function .

success: function(data) {
drawChart(data);
},

function drawChart(data)
{
for(var i = 0 ; i < data.jobs.length ; i++){

alert(data.jobs.id[i]); // This is not working , please help . Thank you .
}
}[/code]


Save India From Corruption - Anna Hazare.
Nikhil Wanpal
Greenhorn

Joined: Apr 16, 2010
Posts: 11

As per the code you have written here, you have not parsed the json data obtained into a javascript object, and so "data.jobs.id[i]" will have no meaning as yet. Check my response to you previous post: http://www.coderanch.com/t/528797/HTML-JavaScript/parse-JSON-Object-javascript
You can try this, just to check: var myJSObj = eval(data); and then loop over the new object and alert the values.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Thanks Nikhil I will try
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Now i tried this way as


My JSOn response what i am getting in firebug response as

jobs:[{id:"2009,06,16",title:"2"},{id:"2009,06,18",title:"4"},{id:"2009,06,20",title:"6"},{id:"2009,06,22",title:"8"},{id:"2009,06,24",title:"10"},{id:"2009,06,26",title:"12"}]}

I got this error in Firebug


missing ] after element list
[Break On This Error] ([object Object])


please help , i am struck .
Nikhil Wanpal
Greenhorn

Joined: Apr 16, 2010
Posts: 11

See if this example explains it any better:



...and in your post above you probably have missed the first '{' before "jobs".
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Thank you Nikhil , your responses were fast .

Anyhow i solved this by this way :


$.each(data.jobs, function(index,job) {

PurchasesData.setValue(index, 0, new Date(job.id));
PurchasesData.setValue(index, 1, parseInt(job.title));

}
);
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Proper JSON has quotes



If the quotes are missing, JavaScript's JSON will not parse the string into an object.

Eric
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Unable to extract JSON Response in JQuery function