4

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?

NibblyPig
  • 51,118
  • 72
  • 200
  • 356

3 Answers3

2

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
  • 5,868
  • 5
  • 48
  • 72
  • So is there a native serializer in javascript to serialize to JSON? I want to avoid more plugins where possible, especially dlls for .net – NibblyPig Oct 01 '10 at 08:56
  • JSON IS javascript. it stands for JavaScript Object Notation. In asp.net you might need a dll. – Stefanvds Oct 01 '10 at 09:03
  • 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? – NibblyPig Oct 01 '10 at 09:11
  • Updated link: [http://nateirwin.net/blog/json-array-to-c-using-jsonnet.html](http://nateirwin.net/blog/json-array-to-c-using-jsonnet.html) – chprpipr Jun 19 '12 at 21:42
2

Check this answer 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

Community
  • 1
  • 1
Zuhaib
  • 1,420
  • 3
  • 18
  • 34