一、添加引用(using Newtonsoft.Json.Linq;)
二、
1.生成json字符串源码
List<string> list = new List<string>();
list.Add("");
list.Add("");
list.Add("");
list.Add("");
list.Add(""); JObject obj1 = new JObject();
JArray array = new JArray();
JObject objdatas = null; for (int i = ; i < ; i++)//第一层
{
objdatas = new JObject();
JObject obj2 = null;
JObject obj3 = new JObject();
for (int j= ; j < list.Count; j++)
{
obj2 = new JObject();
obj2.Add(new JProperty("数据" + (j + ).ToString() + "", list[j]));
obj3.Add("第"+(j+).ToString()+"小层",obj2);
}
objdatas.Add(new JProperty("第" + (i + ).ToString() + "层", obj3));
array.Add(objdatas);
}
obj1.Add("datas", array);
JObject objEnd = new JObject();
objEnd.Add("Datas",obj1); txtdata.Text = objEnd.ToString();
2.得到的json字符串示例
三、反序列化json数据
JObject obj = JObject.Parse(strJson);
JToken tok = obj["Datas"];
JObject objdata = JObject.Parse(tok.ToString());
JToken tokdatas = null;
StringBuilder txt = new StringBuilder();
for (int i = ; i < objdata["datas"].Count(); i++)
{
tokdatas = objdata["datas"][i];
foreach (JProperty item in tokdatas)
{
txt.Append("第" + (i + ).ToString() + "条数据:");
txt.Append(item.Value["第1小层"]["数据1"].ToString() + "&&");
txt.Append(item.Value["第2小层"]["数据2"].ToString() + "&&");
txt.Append(item.Value["第3小层"]["数据3"].ToString() + "&&");
txt.Append(item.Value["第4小层"]["数据4"].ToString() + "&&");
txt.Append(item.Value["第5小层"]["数据5"].ToString());
}
}