views:

54

answers:

3

I have a custom Javascript object that has a few string and float members. I'd like to serialize an array of them in Javascript, assign the result to a hidden field, and then retrieve and deserialize them in the codebehind for my asp.net application.

Is there a standard way to do this?

+2  A: 

what you need is JSON.

both javascript and C# can handle it.

article: http://www.nateirwin.net/2008/11/20/json-array-to-c-using-jsonnet/

also: http://www.json.org/ (scroll to the end)

Stefanvds
So is there a native serializer in javascript to serialize to JSON? I want to avoid more plugins where possible, especially dlls for .net
SLC
JSON IS javascript. it stands for JavaScript Object Notation. In asp.net you might need a dll.
Stefanvds
So how do I convert a javascript object to JSON, in javascript, so I can pop it into a hidden field and handle it on submit?
SLC
just look in the page under javascript. or: http://json.org/json2.js
Stefanvds
+2  A: 

Check this answer http://stackoverflow.com/questions/331976/how-do-i-serialize-a-c-anonymous-type-to-a-json-string

Edit:
Since JavaScriptSerializer is deprecated .. you can use DataContractJsonSerializer. MSDN: DataContractJsonSerializer Blog post on how to use it: http://pietschsoft.com/post/2008/02/NET-35-JSON-Serialization-using-the-DataContractJsonSerializer.aspx

Zuhaib