Any idea on how to do it? If not possible, what's a good JSON library for C#?
+4
A:
Json.NET is a great .NET json library. Suports LINQ, reading/writing and converting objects to and from json.
Luke Lowrey
2009-04-22 05:06:03
I looked at it. Seems way too enterprisey for compared to System.Json. I'm mainly looking to use Json to serialize and deserialize lists of implicit datastructures (tuples, etc.). I'm working mostly dynamic data already, so its ability to serialize strongly typed objects isn't exactly something I'm thrilled about, and its other method is overly verbose.
Patrick
2009-04-22 05:56:08
It does dynamic data just like System.Json in as well as serializing/deserializing:JObject o = JObject.Parse("{'first_name':'Jeff', 'age':30}");Console.WriteLine(o["first_name"]);
James Newton-King
2009-04-26 02:19:42
That is better. I really liked the implicit operators on the JsonValue class. I may just give it a shot.
Patrick
2009-04-26 05:40:23
A:
If you're just looking for JSON encoding/decoding, there is an official System.Web extension library from Microsoft that does it, odds are you probably already have this assembly (System.Web.Extensions):
http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx
JonoW
2009-04-22 09:11:20
A:
Here's an extenstion method to serialize any object instance to JSON:
public static class GenericExtensions
{
public static string ToJsonString<T>(this T input)
{
string json;
DataContractJsonSerializer ser = new DataContractJsonSerializer(input.GetType());
using (MemoryStream ms = new MemoryStream())
{
ser.WriteObject(ms, input);
json = Encoding.Default.GetString(ms.ToArray());
}
return json;
}
}
You'll need to add a reference to System.ServiceModel.Web to use the DataContractSerializer.
Paul Suart
2009-04-22 09:24:16