wood burning stoves 2.0*
The moose likes HTML, CSS and JavaScript and the fly likes Encoding  javascript object  to JSON Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Encoding  javascript object  to JSON" Watch "Encoding  javascript object  to JSON" New topic
Author

Encoding javascript object to JSON

Joel Bushart
Greenhorn

Joined: Aug 11, 2005
Posts: 11
Hey fellow ranchers,

I have a JavaScript object that I'd like to encode into JSON for Ajax transmission.

Does anyone know a JS framework that has a function that will translate a JSObject to JSON and back?

Pseudo-example:

[ October 13, 2006: Message edited by: Joel Bushart ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

Usually JSON is generated on the server to be sent to the client. How are you planning on using with Ajax from the client?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Joel Bushart
Greenhorn

Joined: Aug 11, 2005
Posts: 11
Basically I've got a Google maps marker that I want to be able to assign contents provided by the server for it's infowindow, but only when a user clicks on it.

So, I have the marker created and I want to send that marker object to the server. Then send it back so I can assign the info window data to the current marker object.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

So you need to send the data to the server as JSON? (Reason I ask is that I know of a few serve-side libraries to do this, but not client-side).

On the other hand, it shouldn't be all that hard to code one up...
Joel Bushart
Greenhorn

Joined: Aug 11, 2005
Posts: 11
Originally posted by Bear Bibeault:
So you need to send the data to the server as JSON? (Reason I ask is that I know of a few serve-side libraries to do this, but not client-side).

On the other hand, it shouldn't be all that hard to code one up...


yep, though I was hopeing someone had already done one I could "borrow"
Joel Bushart
Greenhorn

Joined: Aug 11, 2005
Posts: 11
After a couple of attempts, a better solution turned out to be wrapping the object in another wrapper object to preserve it and use object methods in the wrapper to handle the ajax operations.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Have you looked at JSON.ORG

Eric
Joel Bushart
Greenhorn

Joined: Aug 11, 2005
Posts: 11
Yep, it helped a lot on the implementation specifics.

However, I found it leads to memory leaks, as I have an object being created by a third party that I was intending to transmit as JSON then update it with some info received via Ajax.

The update part just seems to add another object and won't match up in memory correctly.

However, using a wrapper allows me to save the memory pointer properly so that I am assured of addressing the same object, also reduces network overhead as I only need to send relevant parts of the data object instead of all of it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Encoding javascript object to JSON