一、添加引用(using Newtonsoft.Json.Linq;)
二、
1.生成json字符串源码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 List<string> list = new List<string>(); 2 list.Add("11111"); 3 list.Add("22222"); 4 list.Add("33333"); 5 list.Add("44444"); 6 list.Add("55555"); 7 8 JObject obj1 = new JObject(); 9 JArray array = new JArray(); 10 JObject objdatas = null; 11 12 for (int i = 0; i < 2; i++)//第一层 13 { 14 objdatas = new JObject(); 15 JObject obj2 = null; 16 JObject obj3 = new JObject(); 17 for (int j= 0; j < list.Count; j++) 18 { 19 obj2 = new JObject(); 20 obj2.Add(new JProperty("数据" + (j + 1).ToString() + "", list[j])); 21 obj3.Add("第"+(j+1).ToString()+"小层",obj2); 22 } 23 objdatas.Add(new JProperty("第" + (i + 1).ToString() + "层", obj3)); 24 array.Add(objdatas); 25 } 26 obj1.Add("datas", array); 27 JObject objEnd = new JObject(); 28 objEnd.Add("Datas",obj1); 29 30 31 txtdata.Text = objEnd.ToString();
2.得到的json字符串示例
三、反序列化json数据
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 JObject obj = JObject.Parse(strJson); 2 JToken tok = obj["Datas"]; 3 JObject objdata = JObject.Parse(tok.ToString()); 4 JToken tokdatas = null; 5 StringBuilder txt = new StringBuilder(); 6 for (int i = 0; i < objdata["datas"].Count(); i++) 7 { 8 tokdatas = objdata["datas"][i]; 9 foreach (JProperty item in tokdatas) 10 { 11 txt.Append("第" + (i + 1).ToString() + "条数据:"); 12 txt.Append(item.Value["第1小层"]["数据1"].ToString() + "&&"); 13 txt.Append(item.Value["第2小层"]["数据2"].ToString() + "&&"); 14 txt.Append(item.Value["第3小层"]["数据3"].ToString() + "&&"); 15 txt.Append(item.Value["第4小层"]["数据4"].ToString() + "&&"); 16 txt.Append(item.Value["第5小层"]["数据5"].ToString()); 17 } 18 }