Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Encoding javascript object to JSON

 
Joel Bushart
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64683
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually JSON is generated on the server to be sent to the client. How are you planning on using with Ajax from the client?
 
Joel Bushart
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64683
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you looked at JSON.ORG

Eric
 
Joel Bushart
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic