5

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

1
  • 1
    As an addendum to this question, can anyone state whether System.Web.Script.Serialization.JavaScriptSerializer is applicable to this question (msdn.microsoft.com/en-us/library/…)? I'm very curious. Commented Nov 12, 2009 at 3:53

5 Answers 5

8

JSON.Net is a pretty good library

Sign up to request clarification or add additional context in comments.

1 Comment

JSON.Net all the way , makes working with json so much easier
4
var jss = new JavaScriptSerializer();
var data = jss.Deserialize<dynamic>(jsonString);

Don't forget to reference "System.Web.Extensions"

Comments

2

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

Comments

1

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();

Comments

0

DataContractJsonSerializer for serializing to/from objects.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.