首页 > Web开发 > 详细

Newtonsoft.Json.4.5.0 序列化与反序列化json字符串使用方法总结

时间:2014-05-22 04:58:44      阅读:501      评论:0      收藏:0      [点我收藏+]

在VS里下载安装:

Json.NET 5.0.6

http://www.nuget.org/packages/Newtonsoft.Json/5.0.6

反序列化json字符串为List:

json字符串如下:

bubuko.com,布布扣
 1 {"docs": [{
 2             "uid": 2629577,
 3             "nickname": "枕边疯电台",
 4             "is_v": "true",
 5             "category_id": 4,
 6             "cover_path": "http://fdfs.xmcdn.com/group3/M01/64/3B/wKgDslJ6DkyQb4EXAAEXoFUKDKE711_mobile_small.jpg",
 7             "intro": "业余主持,纯属兴趣,更新不定时,长短没限制,自嘲只为传播欢乐,不要结合现实对号入座。喜欢请多给我一点鼓励,如果不喜欢也希望得到中肯的建议。如果实在不和您的胃口,请移步到八丈以外的瑶池仙境,这里是人间,不适合您……实在要喷,请注意下自己的素质和言行,只是善意提醒,损人是一件灰常折寿的事情。     ",
 8             "user_source": "1",
 9             "id": 203355,
10             "title": "段子来了",
11             "updated_at": 1399973243000,
12             "created_at": 1376650745000,
13             "avatar_path": "http://fdfs.xmcdn.com/group3/M00/15/65/wKgDslIN_pjwNb5UAACBgMAPyaM325_mobile_small.jpg",
14             "tracks": 131
15         },
16         {
17             "uid": 2629294,
18             "nickname": "采采",
19             "is_v": "true",
20             "category_id": 4,
21             "cover_path": "http://fdfs.xmcdn.com/group3/M04/64/9D/wKgDsVJ6DnSy_6Q7AAEXoFUKDKE679_mobile_small.jpg",
22             "intro": "",
23             "user_source": "1",
24             "id": 214706,
25             "title": "段子来了 采采",
26             "updated_at": 1400250058000,
27             "created_at": 1382348506000,
28             "avatar_path": "http://tp3.sinaimg.cn/1263078850/180/5623249090/0",
29             "tracks": 127
30         },
31         {
32             "uid": 4498275,
33             "nickname": "心的节拍",
34             "is_v": "true",
35             "category_id": 2,
36             "cover_path": "http://fdfs.xmcdn.com/group3/M04/9A/49/wKgDsVKvMk7hRm8KAAEJx49MS9s693_mobile_small.jpg",
37             "intro": "喜马拉雅 枕边疯电台、段子来了--背景音乐 【 不 断 更 新 。 多 谢 关 注、采迷们、、】    喜欢就点个赞 转个采 评个论 你们的支持~ 是我的动力! 谢谢  ",
38             "user_source": "2",
39             "id": 223871,
40             "title": "【段子来了-背景音乐】-全集",
41             "updated_at": 1400343573000,
42             "created_at": 1385138688000,
43             "avatar_path": "http://fdfs.xmcdn.com/group3/M00/7A/C2/wKgDslKPYpjxneH5AAAdDa0wExk487_mobile_small.gif",
44             "tracks": 35
45         },
46         {
47             "category_id": 4,
48             "intro": "江湖盛事,凰泉带着段子来了,小伙伴们,快点开听吧~~~~ ",
49             "cover_path": "http://fdfs.xmcdn.com/group3/M00/82/75/wKgDslKYCe3CEs0KAAHJisvwlLA043_mobile_small.jpg",
50             "user_source": "1",
51             "id": 217022,
52             "title": "江湖盛事《 凰泉来了,段子来了!! 》",
53             "updated_at": 1387614900000,
54             "created_at": 1383301247000,
55             "uid": 3963423,
56             "is_v": "true",
57             "nickname": "烟雨凰阳丶有声电台",
58             "avatar_path": "http://fdfs.xmcdn.com/group5/M00/0B/58/wKgDtlN0Ds2QM4ubAARIhX4zPC0599_mobile_small.jpg",
59             "tracks": 31
60         },
61         {
62             "uid": 6360654,
63             "is_v": "true",
64             "id": 253901,
65             "title": "段子“又”来了",
66             "created_at": 1399210519000,
67             "avatar_path": "http://fdfs.xmcdn.com/group3/M0A/4D/0C/wKgDslNH47zjf0AsAACSWLi3c04438_mobile_small.jpg",
68             "nickname": "小宇说大话",
69             "category_id": 4,
70             "intro": "段子又来了是以简短的形式出现的!但每个段子都是爆笑的!喜欢的点个赞,不喜欢的也勿喷!想关注小宇的朋友们,可以在腾讯微博里搜索小宇傻笑!!!也可以在新浪微博里搜索做人之道!!!谢谢朋友们对小宇的支持!!!小宇说大话的微信公众平台号AA686,在微信搜索公众号AA686就可以找到我们的节目了!!!想与主播小宇零距离互动吗?就去微信搜索关注吧!",
71             "cover_path": "http://fdfs.xmcdn.com/group4/M02/55/2F/wKgDtFNmQdTRtaZDAAECKaQdb8s505_mobile_small.jpg",
72             "user_source": "1",
73             "updated_at": 1399210515000,
74             "tracks": 12
75         }]
76     }
View Code 

C#代码:

AlbumInfo

bubuko.com,布布扣
 1 public class AlbumInfo
 2     {
 3        public AlbumInfo() { }
 4         public int category_id { get; set; }
 5         public string cover_path { get; set; }
 6         public string intro { get; set; }
 7         public string user_source { get; set; }
 8         public int id { get; set; }
 9         public string title { get; set; }
10         public object updated_at { get; set; }
11         public object created_at { get; set; }
12         public int uid { get; set; }
13         public string is_v { get; set; }
14         public string nickname { get; set; }
15         public string avatar_path { get; set; }
16         public int tracks { get; set; }
17 
18     }
View Code

 方式一:

bubuko.com,布布扣
 1 string contents = " {‘docs‘: [{
 2             uid: 2629577,
 3             nickname: 枕边疯电台,
 4             is_v: true,
 5             category_id: 4,
 6             cover_path: http://fdfs.xmcdn.com/group3/M01/64/3B/wKgDslJ6DkyQb4EXAAEXoFUKDKE711_mobile_small.jpg,
 7             intro: 业余主持,纯属兴趣,更新不定时,长短没限制,自嘲只为传播欢乐,不要结合现实对号入座。喜欢请多给我一点鼓励,如果不喜欢也希望得到中肯的建议。如果实在不和您的胃口,请移步到八丈以外的瑶池仙境,这里是人间,不适合您……实在要喷,请注意下自己的素质和言行,只是善意提醒,损人是一件灰常折寿的事情。     ,
 8             user_source: 1,
 9             id: 203355,
10             title: 段子来了,
11             updated_at: 1399973243000,
12             created_at: 1376650745000,
13             avatar_path: http://fdfs.xmcdn.com/group3/M00/15/65/wKgDslIN_pjwNb5UAACBgMAPyaM325_mobile_small.jpg,
14             tracks: 131
15         },
16         {
17             uid: 2629294,
18             nickname: 采采,
19             is_v: true,
20             category_id: 4,
21             cover_path: http://fdfs.xmcdn.com/group3/M04/64/9D/wKgDsVJ6DnSy_6Q7AAEXoFUKDKE679_mobile_small.jpg,
22             intro: ‘‘,
23             user_source: 1,
24             id: 214706,
25             title: 段子来了 采采,
26             updated_at: 1400250058000,
27             created_at: 1382348506000,
28             avatar_path: http://tp3.sinaimg.cn/1263078850/180/5623249090/0,
29             tracks: 127
30         },
31         {
32             uid: 4498275,
33             nickname: 心的节拍,
34             is_v: true,
35             category_id: 2,
36             cover_path: http://fdfs.xmcdn.com/group3/M04/9A/49/wKgDsVKvMk7hRm8KAAEJx49MS9s693_mobile_small.jpg,
37             intro: 喜马拉雅 枕边疯电台、段子来了--背景音乐 【 不 断 更 新 。 多 谢 关 注、采迷们、、】    喜欢就点个赞 转个采 评个论 你们的支持~ 是我的动力! 谢谢  ,
38             user_source: 2,
39             id: 223871,
40             title: 【段子来了-背景音乐】-全集,
41             updated_at: 1400343573000,
42             created_at: 1385138688000,
43             avatar_path: http://fdfs.xmcdn.com/group3/M00/7A/C2/wKgDslKPYpjxneH5AAAdDa0wExk487_mobile_small.gif,
44             tracks: 35
45         },
46         {
47             category_id: 4,
48             intro: 江湖盛事,凰泉带着段子来了,小伙伴们,快点开听吧~~~~ ,
49             cover_path: http://fdfs.xmcdn.com/group3/M00/82/75/wKgDslKYCe3CEs0KAAHJisvwlLA043_mobile_small.jpg,
50             user_source: 1,
51             id: 217022,
52             title: 江湖盛事《 凰泉来了,段子来了!! 》,
53             updated_at: 1387614900000,
54             created_at: 1383301247000,
55             uid: 3963423,
56             is_v: true,
57             nickname: 烟雨凰阳丶有声电台,
58             avatar_path: http://fdfs.xmcdn.com/group5/M00/0B/58/wKgDtlN0Ds2QM4ubAARIhX4zPC0599_mobile_small.jpg,
59             tracks: 31
60         },
61         {
62             uid: 6360654,
63             is_v: true,
64             id: 253901,
65             title: 段子“又”来了,
66             created_at: 1399210519000,
67             avatar_path: http://fdfs.xmcdn.com/group3/M0A/4D/0C/wKgDslNH47zjf0AsAACSWLi3c04438_mobile_small.jpg,
68             nickname: 小宇说大话,
69             category_id: 4,
70             intro: 段子又来了是以简短的形式出现的!但每个段子都是爆笑的!喜欢的点个赞,不喜欢的也勿喷!想关注小宇的朋友们,可以在腾讯微博里搜索小宇傻笑!!!也可以在新浪微博里搜索做人之道!!!谢谢朋友们对小宇的支持!!!小宇说大话的微信公众平台号AA686,在微信搜索公众号AA686就可以找到我们的节目了!!!想与主播小宇零距离互动吗?就去微信搜索关注吧!,
71             cover_path: http://fdfs.xmcdn.com/group4/M02/55/2F/wKgDtFNmQdTRtaZDAAECKaQdb8s505_mobile_small.jpg,
72             user_source: 1,
73             updated_at: 1399210515000,
74             tracks: 12
75         }]
76     }";
77 JObject resutlJobj = JObject.Parse(contents);
78 // serialize JSON results into .NET objects
79 IList < AlbumInfo > searchResults = JsonConvert.DeserializeObject < List < AlbumInfo >> (resutlJobj["docs"].ToString());
View Code

 

 方式二:

bubuko.com,布布扣
 1 JObject resutlJobj = JObject.Parse(contents);
 2 // get JSON result objects into a list
 3 IList < JToken > results = resutlJobj["docs"].Children().ToList();
 4 
 5 // serialize JSON results into .NET objects
 6 IList < AlbumInfo > searchResults = new List < AlbumInfo > ();
 7 //new List<AlbumInfo>();
 8 foreach(JToken result in results) {
 9 AlbumInfo searchResult = JsonConvert.DeserializeObject < AlbumInfo > (result.ToString());
10 searchResults.Add(searchResult);
11 }
View Code

 

序列化对象为json字符串:

 

string json = JsonConvert.SerializeObject(searchResults);

 

Newtonsoft.Json.4.5.0 序列化与反序列化json字符串使用方法总结,布布扣,bubuko.com

Newtonsoft.Json.4.5.0 序列化与反序列化json字符串使用方法总结

原文:http://www.cnblogs.com/lulee007/p/3738279.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!