Created
May 15, 2011 19:28
-
-
Save amithgeorge/973457 to your computer and use it in GitHub Desktop.
In response to http://stackoverflow.com/questions/6009603/parsing-json-using-system-web-script-seriarilization-or-using-json-net/6010001#6010001
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Rewrite the sdata class as follows. | |
*/ | |
public class sdata | |
{ | |
public string id { get; set; } | |
public sfrom from { get; set; } | |
public string message { get; set; } | |
public string updated_time { get; set; } | |
public likedatacollection likes { get; set; } | |
} | |
/* | |
add the likedatacollection class | |
*/ | |
public class likedatacollection | |
{ | |
public List<likesdata> data { get; set; } | |
} | |
/* | |
The other classes remain as they are | |
*/ | |
public class Allstatus | |
{ | |
public List<sdata> data { get; set; } | |
//public scpaging paging { get; set; } | |
} | |
public class likesdata | |
{ | |
public string id { get; set; } | |
public string name { get; set; } | |
} | |
public class sfrom | |
{ | |
public string name { get; set; } | |
public string category { get; set; } | |
public string id { get; set; } | |
} | |
/* | |
the Allstatus object now looks like this | |
*/ | |
var allstatus = new Allstatus | |
{ | |
data = new List<sdata>() | |
{ | |
new sdata() | |
{ | |
from = new sfrom() {category = "message", id = "192", name = "Anthony"}, | |
id = "1000", | |
message = "this is the message", | |
updated_time = "2001-05-06T19:34:15+0000", | |
likes = new likedatacollection() | |
{ | |
data = new List<likesdata>() | |
{ | |
new likesdata() | |
{ | |
id = "100001692250255", | |
name = "\u00dcnal Turanl\u0131" | |
}, | |
new likesdata() | |
{ | |
id = "100001060078996", | |
name = "S\u00e9f\u00e2 K\u00e2ql\u00e4Nn" | |
} | |
} | |
} | |
}, | |
new sdata() | |
{ | |
from = new sfrom() | |
{ | |
category = "message", | |
id = "10001", | |
name = "another name" | |
}, | |
id = "10150186255753553", | |
message = "this is the message", | |
updated_time = "2001-04-06T19:34:15+0000", | |
likes = new likedatacollection() | |
{ | |
data = new List<likesdata>() | |
{ | |
new likesdata() | |
{ | |
id = "1002345", | |
name = "\u00dcnal Turanl\u0131" | |
}, | |
new likesdata() | |
{ | |
id = "100234", | |
name = "S\u00e9f\u00e2 K\u00e2ql\u00e4Nn" | |
} | |
} | |
} | |
} | |
} | |
}; | |
/* | |
Serialize using JavascriptSerializer | |
*/ | |
var javaScriptSerializer = new JavaScriptSerializer(); | |
var serialized = javaScriptSerializer.Serialize(allstatus); | |
/* | |
And the above object after being serialized will look like this. | |
*/ | |
/* | |
{ | |
"data": [ | |
{ | |
"id": "1000", | |
"from": { | |
"name": "Anthony Waema", | |
"category": "message", | |
"id": "192" | |
}, | |
"message": "this is the message", | |
"updated_time": "2001-05-06T19:34:15+0000", | |
"likes": { | |
"data": [ | |
{ | |
"id": "100001692250255", | |
"name": "Ünal Turanli" | |
}, | |
{ | |
"id": "100001060078996", | |
"name": "Séfâ KâqläNn" | |
} | |
] | |
} | |
}, | |
{ | |
"id": "10150186255753553", | |
"from": { | |
"name": "another name", | |
"category": "message", | |
"id": "100001" | |
}, | |
"message": "this is the message", | |
"updated_time": "2001-04-06T19:34:15+0000", | |
"likes": { | |
"data": [ | |
{ | |
"id": "1002345", | |
"name": "Ünal Turanli" | |
}, | |
{ | |
"id": "100234", | |
"name": "Séfâ KâqläNn" | |
} | |
] | |
} | |
} | |
] | |
} | |
*/ | |
/* | |
And it can be deserialized using | |
*/ | |
var deserializedAllStatus = javaScriptSerializer.Deserialize<Allstatus>(serialized); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment