tags:

views:

1515

answers:

4

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
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
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
That is better. I really liked the implicit operators on the JsonValue class. I may just give it a shot.
Patrick
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
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