RSS

Monthly Archives: December 2011

JSON And C# Object Serialization Quick Reference

With JavaScriptSerializer, you can pretty much serialize / deserialize json-formatted string to any object and vice versa. Some of example below use class object, you can find this class object at the very bottom.

Here’s the quick reference:

string[] _testJsonStringArray = new string[8];
_testJsonStringArray[0] = "Id";
_testJsonStringArray[1] = "Firstname";
_testJsonStringArray[2] = "Lastname";
_testJsonStringArray[3] = "Age";
_testJsonStringArray[4] = "Job Description";
_testJsonStringArray[5] = "Address";
_testJsonStringArray[6] = "Email";
_testJsonStringArray[7] = "Hometown";

// Serialize Output:
// ["Id","Firstname","Lastname","Age","Job Description","Address","Email","Hometown"]
IEnumerable _testJsonList = new List() {
    "Id", "Firstname", "Lastname", "Age", "Job Description", "Address", "Email", "Hometown"
};

// Serialize Output:
// ["Id","Firstname","Lastname","Age","Job Description","Address","Email","Hometown"]
IEnumerable> _testJsonNestedList = new List>()
{
    new List()  {
        "31", "Test"
    },
    new List(){
        "12", "Also Test"
    }
};

// Serialize Output:
// [["31","Test"],["12","Also Test"]]
Person _testJsonClass = new Person()
{
    Firstname = "Alexander",
    Lastname = "The Great",
    Age = 99,
    JobDescription = "Eat and sleep",
    Address = "11 NoAddress St",
    Email = "alexander.thegreat@thegreat.com",
    Birthday = new DateTime(1999, 1, 1),
    Hometown = "Nowhere"
};

// Serialize Output:
// {"Firstname":"Alexander","Lastname":"The Great","Age":99,"JobDescription":"Eat and sleep","Address":"11 NoAddress St","Email":"alexander.thegreat@thegreat.com","Birthday":"\/Date(915170400000)\/","Hometown":"Nowhere"}
IEnumerable _testJsonClassCollection = new List()
{
    new Person() {
        Firstname = "Alexander",
        Lastname = "The Great",
        Age = 99,
        JobDescription = "Eat and sleep",
        Address = "11 NoAddress St",
        Email = "alexander.thegreat@thegreat.com",
        Birthday = new DateTime(1999, 1, 1),
        Hometown = "Nowhere"
    },
    new Person() {
        Firstname = "Smith",
        Lastname = "The Great",
        Age = 88,
        JobDescription = "Shopping and coding",
        Address = "12 NoAddress St",
        Email = "smith.thegreate@thegreate.com",
        Birthday = new DateTime(1980, 1, 1),
        Hometown = "Somewhere"
    }
};

// Serialize Output:
// [{"Firstname":"Alexander","Lastname":"The Great","Age":99,"JobDescription":"Eat and sleep","Address":"11 NoAddress St","Email":"alexander.thegreat@thegreat.com","Birthday":"\/Date(915170400000)\/","Hometown":"Nowhere"},{"Firstname":"Smith","Lastname":"The Great","Age":88,"JobDescription":"Shopping and coding","Address":"12 NoAddress St","Email":"smith.thegreate@thegreate.com","Birthday":"\/Date(315554400000)\/","Hometown":"Somewhere"}]
Building _testJsonClassContainCollection = new Building()
{
    Rooms = new List() {
        "401",
        "213",
        "523"
    }
};

// Serialize Output:
// {"Rooms":["401","213","523"]}
IEnumerable _testJsonClassCollectionContainCollection = new List()
{
    new Building() {
        Rooms = new List() { "White", "Blue", "Red" }
    },
    new Building() {
        Rooms = new List() { "A23", "23C", "G34" }
    }
};

// Serialize Output:
// [{"Rooms":["White","Blue","Red"]},{"Rooms":["A23","23C","G34"]}]
NestedBuilding _testJsonClassNestedList = new NestedBuilding()
{
    Rooms = new List>() {
        new List() { "Blue Room", "Orange Room", "Gray Room" },
        new List() { "Hot Room", "Cold Room", "Warm Room" }
    }
};

// Serialize Output:
// {"Rooms":[["Blue Room","Orange Room","Gray Room"],["Hot Room","Cold Room","Warm Room"]]}

And here’s the classes I used:

public class Person
{
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public int Age { get; set; }
    public string JobDescription { get; set; }
    public string Address { get; set; }
    public string Email { get; set; }
    public DateTime Birthday { get; set; }
    public string Hometown { get; set; }
}

public class Building
{
    public IEnumerable Rooms { get; set; }
}

public class NestedBuilding
{
    public IEnumerable> Rooms { get; set; }
}
 
1 Comment

Posted by on December 1, 2011 in General

 

Tags: , ,

 
%d bloggers like this: