c#简单的Json解析类
使用方法:
引用Newtonsoft.Json.dll文件,然后引用命名空间using Newtonsoft.Json.Linq;
JsonDome中有实例,照做就行
现在贴上示例代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using Newtonsoft.Json.Linq; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace JD 9 { 10 public class JsonDome 11 { 12 public static Dictionary<string, string> GetJsonstr(string jsonStr) 13 { 14 jsonStr = "{\"category_attribute_search_response\":{\"code\":\"0\",\"total\":35,\"attributes\":[{\"aid\":10093066,\"name\":\"品相\",\"cid\":\"3837\"},{\"aid\":83470,\"name\":\"阶段\",\"cid\":\"3837\"},{\"aid\":10044550,\"name\":\"编纂\",\"cid\":\"3837\"},{\"aid\":10044551,\"name\":\"摄影\",\"cid\":\"3837\"},{\"aid\":10044548,\"name\":\"口述\",\"cid\":\"3837\"},{\"aid\":10044549,\"name\":\"朗读\",\"cid\":\"3837\"},{\"aid\":10044546,\"name\":\"校对\",\"cid\":\"3837\"},{\"aid\":10044547,\"name\":\"注释\",\"cid\":\"3837\"},{\"aid\":10044544,\"name\":\"译者\",\"cid\":\"3837\"},{\"aid\":10044545,\"name\":\"绘者\",\"cid\":\"3837\"},{\"aid\":10044558,\"name\":\"读者对象\",\"cid\":\"3837\"},{\"aid\":10044559,\"name\":\"附件数量\",\"cid\":\"3837\"},{\"aid\":10044556,\"name\":\"中图法分类号\",\"cid\":\"3837\"},{\"aid\":10044557,\"name\":\"主题词\",\"cid\":\"3837\"},{\"aid\":10044554,\"name\":\"品牌\",\"cid\":\"3837\"},{\"aid\":10044555,\"name\":\"附件\",\"cid\":\"3837\"},{\"aid\":10044552,\"name\":\"书写\",\"cid\":\"3837\"},{\"aid\":10044553,\"name\":\"整理\",\"cid\":\"3837\"},{\"aid\":10044536,\"name\":\"字数\",\"cid\":\"3837\"},{\"aid\":10044537,\"name\":\"印次\",\"cid\":\"3837\"},{\"aid\":10044538,\"name\":\"印刷时间\",\"cid\":\"3837\"},{\"aid\":10044539,\"name\":\"用纸\",\"cid\":\"3837\"},{\"aid\":10044540,\"name\":\"丛书名\",\"cid\":\"3837\"},{\"aid\":10044541,\"name\":\"外文名\",\"cid\":\"3837\"},{\"aid\":10044542,\"name\":\"著者\",\"cid\":\"3837\"},{\"aid\":10044543,\"name\":\"编者\",\"cid\":\"3837\"},{\"aid\":10044528,\"name\":\"版次\",\"cid\":\"3837\"},{\"aid\":10044529,\"name\":\"包装 \",\"cid\":\"3837\"},{\"aid\":10044530,\"name\":\"出版社\",\"cid\":\"3837\"},{\"aid\":10044531,\"name\":\"出版时间\",\"cid\":\"3837\"},{\"aid\":10044532,\"name\":\"正文语言\",\"cid\":\"3837\"},{\"aid\":10044533,\"name\":\"开本\",\"cid\":\"3837\"},{\"aid\":10044534,\"name\":\"套装数量\",\"cid\":\"3837\"},{\"aid\":10044535,\"name\":\"页数\",\"cid\":\"3837\"},{\"aid\":10044527,\"name\":\"ISBN\",\"cid\":\"3837\"}]}}"; 15 //先获得名为category_attribute_search_response的值,再从中获得名为attribute的值就得到中括号中的键值对 16 var list = JObject.Parse(jsonStr)["category_attribute_search_response"]["attributes"]; 17 Dictionary<string, string> listJson = new Dictionary<string, string>(); 18 for (int i = 0; i < list.Count(); i++) 19 { 20 if (list[i]["name"].ToString() == "ISBN") 21 { 22 listJson.Add("ISBN", list[i]["aid"].ToString()); 23 } 24 if (list[i]["name"].ToString() == "版次") 25 { 26 listJson.Add("版次", list[i]["aid"].ToString()); 27 } 28 if (list[i]["name"].ToString() == "出版社") 29 { 30 listJson.Add("出版社", list[i]["aid"].ToString()); 31 } 32 } 33 return listJson; 34 } 35 36 } 37 38 }
附上链接地址:https://files.cnblogs.com/valiant1882331/Newtonsoft.Json.rar