tags:

views:

266

answers:

4

Is there an easy/elegant parser for dealing with JSON in C#? How about actually serializing/deserializing into C# objects?

+2  A: 

See

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx

Basically you can use the 'data contract' model (that's often used for WCF XML serialization) for JSON as well. It's pretty quick and easy to use standalone for little tasks, I have found.

Also check out this sample:

http://msdn.microsoft.com/en-us/library/bb943471.aspx

Brian
+6  A: 

JSON.Net is a pretty good library

Simon Fox
JSON.Net all the way , makes working with json so much easier
RC1140
+1  A: 

There's the DataContractJsonSerializer class.

Deserialize:

DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(MyObject));
Stream s = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(json_string));
MyObject obj = ser.ReadObject(s) as MyObject;

Serialize:

DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(MyObject));
Stream s = new MemoryStream();
MyObject obj = new MyObject { .. set properties .. };
ser.WriteObject(s, obj);
s.Seek( SeekOrigin.Begin );
var reader = new StreamReader(s);
string json_string = reader.ReadToEnd();
tvanfosson
A: 

DataContractJsonSerializer for serializing to/from objects.

In Silverlight 3, there's System.Json (http://msdn.microsoft.com/en-us/library/system.json%28VS.95%29.aspx), very handy.

alexdej