Wednesday, October 28, 2009

Deserialize JSON with C#

You're using JQuery, making some fancy ajax requests, but tired of Request["setting"] to get data from client-side, starving for a strong-typed approach? STOP! I've got just what you need, and supports single object, and even lists of objects (arrays)!

Example:

Single: { "field1":"value1","field2":"value2" }
Array: [ { "field1":"value1","field2":"value2" }, { "field1":"value1","field2":"value2" } ]

For the single, the .NET Framework has the key, you need to create a class that has public fields or properties matching the field names on your JSON string, example:

public class Test
{
public string field1 { get; set; }
public string field2 { get; set; }
}

And the code to serialize your Single JSON is this:

Test myDeserializedObj = (Test)JavaScriptConvert.DeserializeObject(Request["jsonString"], typeof(Test));       

and voilá! you got yourself a strong-typed object with client-side data, sweet!


But and if I want a list of data of the same type, say an Array, how is it done? The .NET Framework hasn't got the answer for this one..
You have to download and reference the following DLL

Once it's done:

List< test > myDeserializedObjList = (List< test >)Newtonsoft.Json.JsonConvert.DeserializeObject(Request["jsonString"], typeof(List< test >));

and yes! you're done :)

How easy is this? It's perfect for your lightweight ajax applications with .NET backend.

Please post comments!

See you soon

6 comments:

Adri said...

Nice, worked very well!

Note: the API has changed, now it's Newtonsoft.Json.JsonConvert.DeserializeObject<T>(jsonString);

Ricardo Rodrigues said...

Thanks for the feedback :)
Just updated the post to reflect the API changes, thanks!

João Pacheco said...

Nice and Sweet :D
It's a nice and simple feature :D.
This DLL don't exist for .NET 1.1.
I used this static method [ Dim myDeserializedObj As String = Newtonsoft.Json.JsonConvert.SerializeObject(MyList)

Response.Write(myDeserializedObj)]

And Just Work Fine!
Thanks Bro :D

Nav said...
This comment has been removed by the author.
jibin said...

Hi
Is there any way to achieve the same without using Newtonsoft.....dll from wpf. I want to deserialise json object that contains details of 3 students

DusanVrban said...

What if you have a mix? Like single, then array, then single? And sometimes you have some singles, other times you don't?

Like this:
{
"id": "110770asd445977",
"displayName": "Some Name",
"gender": "male",
"aboutMe": "That nice person",
"image": {
"url": "https://whatever.org/photo.jpg?sz=50"
},
"organizations": [
{
"name": "First org",
"title": "Some title",
"type": "school"
},
{
"name": "Second org",
"title": "Some other title",
"type": "work"
},
]
}

And sometimes you will have gender, other times you won't. Sometimes you will have image, other times you won't.

Does anyone know how to solve this without making 200 if's? If I put this into JSON.net, I guess I will struggle with errors when an x user has no organization.