.net json序列化组件Json.NET

6次阅读

Json.NET(Newtonsoft.Json) 是.Net 框架下比较流行的一款高效 json 序列化开源组件, 支持.Net Framework 2.0 到 4.5+, 并且可用于.Net 各种环境 Asp.net,Silverlight,Windows Phone,Windows 8 等等. 更多特性移步开源 性能 Json.NET、DataContractJsonSerializer、JavascriptSeriallizer 性能测试结果对比,还不错吧。


.net json 序列化组件 Json.NET

引用
方式 1. 下载解压引用 Newtonsoft.Json.dll
方式 2:Nuget 安装
PM> Install-Package Newtonsoft.Json
序列化与反序列
1. 基本用法,首先引用 Newtonsoft.Json 命名空间, 定义好与 json 同结构的的类用于转换
Software software = new Software{SoftID=1,
SoftName=" 限时免费 " ,
DownloadUrl="http://itunes.apple.com/cn/app/id427577372?mt=8",
ReleaseTime=DateTime.Now
};
// 序列化
string jsonStr = JsonConvert.SerializeObject(software);
// 反序列化
Software objSoftware =JsonConvert.DeserializeObject(jsonStr);
Console.WriteLine(jsonStr); 序列化输出

.net json 序列化组件 Json.NET

2. 时间格式处理,DateTime 类型序列化默认序列化如上, 这种格式在其它客户端很难读取,或者想按自己的格式化
Newtonsoft.Json.Converters.IsoDateTimeConverter timeConverter = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
timeConverter.DateTimeFormat = "yyyy 年 MM 月 dd 日 HH:mm:ss";
Console.WriteLine(JsonConvert.SerializeObject(software, timeConverter)); 输出结果:

.net json 序列化组件 Json.NET

3. 匿名序列化, 这种方法无需事先定义与 json 同结构的类就能反序列化
//Json 字符串
string jsonStr = @"{result:-1,desc:' 参数错误,请检查格式 '}";
// 反序列化
var obj = JsonConvert.DeserializeAnonymousType(jsonStr, new { result = 0, desc = string.Empty});
Console.WriteLine(string.Format("result:{0} desc:{1}", obj.result, obj.desc));
4. 快速定位节点,用于快速处理或者 json 结构较为复杂的字符串, 又不想定义对应转移类, 如
{"weatherinfo":{"city":" 福州 ","city_en":"fuzhou","date_y":"2013 年 5 月 4 日 ","date":"","week":" 星期 六 ","fchh":"18","cityid":"101230101","temp1":"16℃~21℃","temp2":"16℃~23℃","temp3":"17℃~24℃","temp4":"16℃~26℃","temp5":"17℃~29℃","temp6":"18℃~28℃","tempF1":"60.8℉~69.8℉","tempF2":"60.8℉~73.4℉","tempF3":"62.6℉~75.2℉","tempF4":"60.8℉~78.8℉","tempF5":"62.6℉~84.2℉","tempF6":"64.4℉~82.4℉","weather1":" 阵雨 ","weather2":" 阵雨转阴 ","weather3":" 阴转雷阵雨 ","weather4":" 阵雨转雷阵 雨 ","weather5":" 阵雨转多云 ","weather6":" 多云转中 雨 ","img1":"3","img2":"99","img3":"3","img4":"2","img5":"2","img6":"4","img7":"3","img8":"4","img9":"3","img10":"1","img11":"1","img12":"8","img_single":"3","img_title1":" 阵雨 ","img_title2":" 阵雨 ","img_title3":" 阵雨 ","img_title4":" 阴 ","img_title5":" 阴 ","img_title6":" 雷阵雨 ","img_title7":" 阵雨 ","img_title8":" 雷阵 雨 ","img_title9":" 阵雨 ","img_title10":" 多云 ","img_title11":" 多 云 ","img_title12":" 中雨 ","img_title_single":" 阵雨 ","wind1":" 微风 ","wind2":" 微 风 ","wind3":" 微风 ","wind4":" 微风 ","wind5":" 微风 ","wind6":" 微风 ","fx1":" 微 风 ","fx2":" 微风 ","fl1":" 小于 3 级 ","fl2":" 小于 3 级 ","fl3":" 小于 3 级 ","fl4":" 小于 3 级 ","fl5":" 小于 3 级 ","fl6":" 小于 3 级 ","index":" 舒适 ","index_d":" 建议着薄型套装或牛仔衫裤等春秋过渡装。年老体弱者宜着套装、夹克衫等。","index48":" 舒适 ","index48_d":" 建议着薄型套装或牛仔衫裤等春秋过渡装。年老体弱者宜着套 装、夹克衫等。","index_uv":" 弱 ","index48_uv":" 最弱 ","index_xc":" 不宜 ","index_tr":" 适 宜 ","index_co":" 舒 适 ","st1":"19","st2":"14","st3":"25","st4":"14","st5":"23","st6":"16","index_cl":" 较不宜 ","index_ls":" 不太适宜 ","index_ag":" 易发 "}} 读取 weatherinfo 下的 weather1
var obj = JObject.Parse(html);
string weather1 = (string)obj["weatherinfo"]["weather1"];

正文完