zoukankan      html  css  js  c++  java
  • JSON字符串序列化与反序列化浅试

    一、添加引用(using Newtonsoft.Json.Linq;)

    二、

    1.生成json字符串源码

     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();
    View Code

    2.得到的json字符串示例

    三、反序列化json数据

     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             }
    View Code
  • 相关阅读:
    Linux Window Redis安装
    Mysql 死锁的详细分析方法
    mariadb rpm 安装
    我希望我能做到:我只是认真--做技术的人,对待技术,应该拥有什么样的态度?
    Google140道面试题
    mysql my.cnf配置文件详解
    Linux iostat字段解析
    Linux mpstat字段解析
    Selenium入门8 js调用
    Selenium入门7 内嵌框架iframe
  • 原文地址:https://www.cnblogs.com/dabexiong/p/5130905.html
Copyright © 2011-2022 走看看