Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Unable to extract JSON Response in JQuery function

 
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]
 
Greenhorn
Posts: 11
jQuery Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: https://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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Nikhil I will try
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
jQuery Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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));

}
);
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Proper JSON has quotes



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

Eric
 
Today's lesson is that you can't wear a jetpack AND a cape. I should have read this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic