JSON.NET(http://json.codeplex.com/)使用来将.NET中的对象转换为JSON字符串(序列化?),或者将JSON字符串转换为.NET中已有类型的对象(反序列化?)@H_404_2@
首先为了例子随便定义一个类型:@H_404_2@
@H_404_2@public@H_404_2@ class@H_404_2@ Product
{
@H_404_2@string@H_404_2@ Name { get@H_404_2@; set@H_404_2@; }
@H_404_2@public@H_404_2@ DateTime Expiry { decimal@H_404_2@ Price { string@H_404_2@[] Sizes { ; }
@H_404_2@override@H_404_2@ string@H_404_2@ ToString()
{
@H_404_2@return@H_404_2@ string@H_404_2@.Format("@H_404_2@Name:{0},Expiry:{1},Price:{2},SizesCount:{3}@H_404_2@"@H_404_2@,Name,Expiry,Price,Sizes.Length);
}
}@H_404_2@
初始化对象:
static@H_404_2@ void@H_404_2@ Main([] passwordargs)
{
Product product @H_404_2@= new@H_404_2@ Product()
{
Name @H_404_2@= android@H_404_2@404_2@= DateTime.Now,Price @H_404_2@= 2000@H_404_2@404_2@= new@H_404_2@ string@H_404_2@[] { 1.5@H_404_2@"@H_404_2@,2.2@H_404_2@4.1@H_404_2@ }
};
}@H_404_2@
进行到JSON的转换:@H_404_2@
Console.WriteLine(JsonConvert.SerializeObject(product));
输出结果:
{"Name":"android","Expiry":"2013-08-30T09:50:11.5147845+08:00","Price":2000.0,"Sizes":["1.5","2.2","4.1"]}
其它看起来一切正常,除了这个日期有点怪
格式化日期:@H_404_2@
//@H_404_2@设置日期时间的格式,与DataTime类型的ToString格式相同@H_404_2@
IsoDateTimeConverter iso = IsoDateTimeConverter();
iso.DateTimeFormat @H_404_2@= yyyy-MM-dd HH:mm:ss@H_404_2@;
Console.WriteLine(JsonConvert.SerializeObject(product,iso));@H_404_2@
输出结果:
{"Name":"android","Expiry":"2013-08-30 09:53:58","4.1"]}
从JSON到对象的转换:@H_404_2@
string@H_404_2@ str = {\"Name\":\"android\",\"Expiry\":\"2013-08-30 09:53:58\",\"Price\":2000.0,\"Sizes\":[\"1.5\",\"2.2\",\"4.1\"]}@H_404_2@;
Product p @H_404_2@= (Product)JsonConvert.DeserializeObject(str,typeof@H_404_2@(Product));
Console.WriteLine(p.ToString());@H_404_2@
输出结果:
Name:android,Expiry:2013/8/30 9:53:58,Price:2000.0,SizesCount:3
从JSON到键值对的转换:@H_404_2@
string@H_404_2@ strJson = @"@H_404_2@{""Name1"": ""小明"",""Name2"": ""小花"",""Name3"": ""小红""}@H_404_2@;
Dictionary@H_404_2@<string@H_404_2@,255)">string@H_404_2@> _dictionary = JsonConvert.DeserializeObject<Dictionary<string@H_404_2@>>(strJson);
@H_404_2@foreach@H_404_2@ (KeyValuePair<string@H_404_2@> kp in@H_404_2@ _dictionary)
{
Console.WriteLine(kp.Key @H_404_2@+ :@H_404_2@"@H_404_2@ + kp.Value);
}@H_404_2@
输出结果:
Name1:小明
Name2:小花
Name3:小红
从字符串转换到JSON对象,以及JSON对象的简单使用:@H_404_2@
string@H_404_2@ strJson2 = { ""student"": { ""Name1"": ""小明"",""Name3"": ""小红""} }@H_404_2@;
JObject jsonObj @H_404_2@= JObject.Parse(strJson2);
Console.WriteLine(jsonObj[@H_404_2@student@H_404_2@"@H_404_2@][Name1@H_404_2@].ToString());
Console.WriteLine(jsonObj[@H_404_2@Name2@H_404_2@Name3@H_404_2@"@H_404_2@].ToString());
输出结果:
小明
小花
小红
@H_404_2@
JObject json =
JObject(
@H_404_2@new@H_404_2@ JProperty(Channel@H_404_2@404_2@ JObject(
@H_404_2@title@H_404_2@JSON@H_404_2@),0)">link@H_404_2@JSON.NET@H_404_2@description@H_404_2@JSON.NET Description@H_404_2@items@H_404_2@ JArray(
@H_404_2@new@H_404_2@ JObject(haha1@H_404_2@123@H_404_2@)),0)">haha2@H_404_2@456@H_404_2@haha3@H_404_2@789@H_404_2@))
)))));
Console.WriteLine(json.ToString());@H_404_2@