Loading ...

convert object to json in c#,convert list to json string,convert object to json,convert collection to json | CodeAsp.Net

C#: Convert to JSON string

 /5
0 (0votes)

Today I was in a need to convert a list of objects to JSON string . I decided to write a blog on what I did to do so. I used JavaScriptSerializer Class which was added in .NET 3.5 version. From MSDN:
The JavaScriptSerializer class is used internally by the asynchronous communication layer to serialize and deserialize the data that is passed between the browser and the Web server. You cannot access that instance of the serializer. However, this class exposes a public API. Therefore, you can use the class when you want to work with JavaScript Object Notation (JSON) in managed code.
To serialize an object to JSON string we can use the Serialize method of this class. I decided to go for an extension method of an object so that I can go for any object which I want to convert to JSON string. Below is the extension class:

using System.Web.Script.Serialization;

public static class JSONExtension
{
    /// <summary>
    /// Convert the object to JSON string
    /// </summary>
    /// <param name="o">The object which is to be converted to JSON string.</param>
    /// <returns>Returns the JSON string.</returns>
    public static string ToJSON(this object o)
    {
        return new JavaScriptSerializer().Serialize(o);
    }
}


Below is the screenshot how that method will appear as an extension:

 
Code snippet to use it:

    protected void Page_Load(object sender, EventArgs e)
    {
        var users = new List<User>
                        {
                          new User{UserID= 1, UserName = "UserName1",FirstName ="FirstName1",LastName = "LastName1" },
                          new User{UserID= 1, UserName = "UserName2",FirstName ="FirstName2",LastName = "LastName2" },
                          new User{UserID= 1, UserName = "UserName3",FirstName ="FirstName3",LastName = "LastName3" },
                          new User{UserID= 1, UserName = "UserName4",FirstName ="FirstName4",LastName = "LastName4" },
                          new User{UserID= 1, UserName = "UserName5",FirstName ="FirstName5",LastName = "LastName5" },
                        };
        string output = users.ToJSON();
        Response.Write(output);
    }


Code of User class used above:

public class User
{
    public int UserID { get; set; }
    public string UserName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}



When you will run the above code you will see this output:



Do let me know your feedback, comments.

 

 

Comments (3)

   
hajan
Nice! I use this way to serialize objects to JSON string very often
1/23/2011
 · 
by
   
kaushi
Hey thanks for that, any idea how to do the reverse, i.e., I have the
JSON and want to convert it back to the List<> object.
2/4/2011
 · 
by
   
raghav_khunger
You can go with JavaScriptSerializer().Deserialize
2/4/2011
 · 
by

Top Posts