Use C# to get JSON data from the web and map it to .NET class => made easy! - CodeProject
http://james.newtonking.com/projects/json/help/index.html?topic=html/SerializingJSON.htm
using native datacontractjsonserializer
http://msdn.microsoft.com/en-us/library/hh674188.aspx
using native .NET JavascriptSerializer
Example code for deserializing the following JSON :
string
res = @" {
""exhibitions"": [
{
""id"": ""1056"",
""name"": ""Alfa Romeo Sustaining Beauty"",
""admission_fee"": false,
""opened_on"": ""2001-12-01"",
""closed_on"": ""2001-05-31"",
""closed"": false,
""website"": null
},
{
""id"": ""1082"",
""name"": ""Agriculture"",
""admission_fee"": false,
""opened_on"": null,
""closed_on"": null,
""closed"": false,
""website"": http://www.sciencemuseum.org.uk/visitmuseum/galleries/agriculture.aspx
} ] }";
// the class property names must match those JSON names
public class Exhibition
{
public int id {get; set;}
public string name { get; set; }
public bool admission_fee { get; set; }
public string opened_on { get; set; }
public string closed_on { get; set; }
public bool closed { get; set; }
public string website { get; set; }
}
public class Response {
public Exhibition[] exhibitions;
}
System.Web.Script.Serialization.JavaScriptSerializer json = new System.Web.Script.Serialization.JavaScriptSerializer();
Response list = json.Deserialize
Example code for serializing objects to JSON string
System.Web.Script.Serialization.JavaScriptSerializer json = new System.Web.Script.Serialization.JavaScriptSerializer();
var e1 = new Exhibition { name = "Test1", closed = true };
var e2 = new Exhibition { name = "Test2", id = 456 };
var r = new Response();
r.exhibitions = new Exhibition[2] { e1, e2 };
string s = json.Serialize(r);
No comments:
Post a Comment