zoukankan      html  css  js  c++  java
  • C# (using Newtonsoft.Json) Json 转换用法小总结

      //序列化

    string Json字符串 = JsonConvert.SerializeObject(目标对象);

     // 字符串转化为对象 

       string UserJson = "{"UNO":"1"," +
                  " "UName":"龙"," +
                  " "Uage":"21"," +
                  " "Uaddress":"中国"," +
                  " "Uphone":"151 3692 3546"}";
                User Data = JsonConvert.DeserializeObject<User>(UserJson);

    //json字符串 转化为List集合

         string jsonText = "{ "Total":"0"," +
                   " "Rows":" +
                    "[" +
                   "{"UNO":"1"," +
                   ""UName":"龙"," +
                   ""Uage":"21"," +
                   ""Uaddress":"中国!"," +
                   ""Uphone":"151 3692 3546"}," +
    
                    "{"UNO":"1"," +
                    ""UName":"龙"," +
                    ""Uage":"21"," +
                    ""Uaddress":"中国!"," +
                    ""Uphone":"151 3692 3546"}" +
                    "]}";
    
    
                //获取索引
                int IndexofA = jsonText.IndexOf("[");
                int IndexofB = jsonText.IndexOf("]");
                //根据索引截取
                string str = jsonText.Substring(IndexofA, IndexofB - IndexofA + 1);
                //序列化
                List<User> objs = JsonConvert.DeserializeObject<List<User>>(str);

    //Json字符串 转化成 DataTable

        string Stringjson = "{ "Total":"0"," +
                 " "Rows":" +
              "[" +
              "{"UNO":"1"," +
               ""UName":"龙"," +
               ""Uage":"21"," +
               ""Uaddress":"中国!"," +
               ""Uphone":"151 3692 3546"}," +
    
               "{"UNO":"1"," +
               ""UName":"龙"," +
               ""Uage":"21"," +
               ""Uaddress":"中国!"," +
               ""Uphone":"151 3692 3546"}" +
               "]}";
    
    
                //获取索引
                int IndexofArrayListA = jsonText.IndexOf("[");
                int IndexofArrayListB = jsonText.IndexOf("]");
                //根据索引截取
                string JsonString = Stringjson.Substring(IndexofArrayListA, IndexofArrayListB - IndexofArrayListA + 1);
                List<User> obj = JsonConvert.DeserializeObject<List<User>>(JsonString);
    
                System.ComponentModel.PropertyDescriptorCollection properties = System.ComponentModel.TypeDescriptor.GetProperties(typeof(User));
                DataTable dt = new DataTable();
                for (int i = 0; i < properties.Count; i++)
                {
                    System.ComponentModel.PropertyDescriptor property = properties[i];
                    dt.Columns.Add(property.Name, property.PropertyType);
                }
                object[] values = new object[properties.Count];
                foreach (User item in obj)
                {
                    for (int i = 0; i < values.Length; i++)
                    {
                        values[i] = properties[i].GetValue(item);
                    }
                    dt.Rows.Add(values);
                }

    //实体对象

     /// <summary>
        /// 实体对象
        /// </summary>
        public class User
        {
            public string UNO { get; set; }
            public string UName { get; set; }
            public string Uage { get; set; }
            public string Uaddress { get; set; }
            public string Uphone { get; set; }
    
        }
  • 相关阅读:
    PHPCMS快速建站系列之添加单页模版
    func_num_args(),func_get_arg(),func_get_args()
    34 个使用 Raspberry Pi 的酷创意
    解决sublime text 2总是在新窗口中打开文件
    帝国cms后台不停的登录成功
    PHP正则表达式的逆向引用与子模式 php preg_replace应用
    【采集】php str_replace
    如何设置让SFTP的用户限制在某个目录下
    win2008修改最大远程桌面连接数
    mysql查询差集
  • 原文地址:https://www.cnblogs.com/szlblog/p/7364828.html
Copyright © 2011-2022 走看看