• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Parsing JSON jQuery object issue

 
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I build the json object in a servlet, then I send it back to the client via ajax. Once back at the browser, I want to get the values. I did search high and low, but I could not find any code that did not throw an error.

Here is the example output transmitted back the client. I do set the application type in the response as 'application\json". I used Google's Simple JSON library.

in JavaScript, if I do a 'alert(jsonObj)' I get what appears to be the correct string:





 
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should not be setting the content-type header unless you are sending JSON, so remove that.

Also, if you want JSON, why did you set the datatype to "text"? Set it to "json". jQuery will then automatically parse the JSON for you.

(Hint: you really should pick up copy of jQuery in Action, 3rd Edition if you are going to be using jQuery. )
 
M Burke
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:You should not be setting the content-type header unless you are sending JSON, so remove that.

Also, if you want JSON, why did you set the datatype to "text"? Set it to "json". jQuery will then automatically parse the JSON for you.

(Hint: you really should pick up copy of jQuery in Action, 3rd Edition if you are going to be using jQuery. )



I appreciate that, thanks.

Also, I find that the json string returned from the server is not formatted correctly. It has '\"' rather than '"' characters. Perhaps I can convert it somehow that makes it valid.

This is an example:
 
Bear Bibeault
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JSON being returned from the server must be valid. If it's not, the server is the place to fix it.

And again, if using jQuery you don't (and can't) parse the returning JSON, it is already converted to a JavaScript object when passed to the handler. I don't understand why you continue to try to parse it.
 
The fastest and most reliable components of any system are those that are not there. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic