tags:

views:

89

answers:

3

Hi, Hoping I don't have to reinvent the wheel here but does anyone know if there is a class in c# similar to the one supplied by Adobe for AS3 to convert a generic object to a JSON string.

For example, when I encode an array of objects. new JSONEncoder(arr).getString();

output: [{"type":"mobile","number":"02-8988-5566"},{"type":"mobile","number":"02-8988-5566"}]

thanks

+2  A: 

The following methods work well for me (using the JavaScriptSerializer):

public static T FromJson<T>(string input)
{
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    return serializer.Deserialize<T>(input);
}

public static string ToJson(object input)
{
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    return serializer.Serialize(input);
}
Fredrik Mörk
+2  A: 

Check this out DataContractJsonSerializer.

Use the DataContractJsonSerializer to serialize and deserialize data in the JavaScript Object Notation (JSON) format. This serialization engine converts JSON data into instances of .NET Framework types and back into JSON data

Rohan West
+5  A: 

in C#:

var jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            string json = jsonSerializer.Serialize(yourCustomObject);
used2could