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; }

    }

 

posted @ 2017-08-15 14:13  乐途  阅读(1621)  评论(0编辑  收藏  举报