Serialize Any .NET Object to a JSON String

The following steps explain how to serialize any .NET object into a JSON string. This tip uses the .NET 3.0 Framework. You’ll find this capability particularly helpful in applications that use AJAX to pass objects from the server to the client code.

Assume you have a Person object defined as shown below:

public class Person{  public int ID { get; set; }  public string FirstName { get; set; }  public string LastName { get; set; }}

Now assume you need to serialize this object to a JSON string to pass it to a client-side script. It turns out you can easily convert an instance of the Person class (or any .NET object) to a JSON string, by using an “extension method” (a capability released with .NET 3.0). To create a JSON serialization extension method, use the following code:

using System.Web.Script.Serialization;namespace JSON.Namespace{  public static class JSONHelper  {     public static string ToJSON(this object obj)     {        JavaScriptSerializer serializer = new JavaScriptSerializer();        return serializer.Serialize(obj);     }  }}

The JavaScriptSerializer class (in System.Web.Script.Serialization) handles the actual object-to-JSON conversion. Note that the ToJSON() extension method above is defined for type Object, which means you can use it with all .NET objects. This means that in addition to calling ToJSON() on a Person object, you could call it the same way on a collection of Person objects or any other .NET datatype. The following examples show how to use the ToJSON() method:

using JSON.Namespace; //Reference the namespace that contains ToJSON()Person person = new Person() {    ID=1, FirstName="Dev",    LastName="Jackson" }; //Create a Person object   //Convert to a JSON string    string JsonString = person.ToJSON();

The output of the preceding code would be:

[{"ID":1, "FirstName":"Dev", "LastName":"Jackson"}]

Conveniently, VS 2008 provides Intellisense and compile-time support for extension methods, just as if they were built-in methods.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.