zoukankan      html  css  js  c++  java
  • Json的序列化与反序列化

    C#Json字符串反序列化成List对象类集合

    using System.IO;
    using System.Web.Script.Serialization;
    using System.Runtime.Serialization.Json;
    public static List<T> JSONStringToList<T>(this string JsonStr)
        {
            JavaScriptSerializer Serializer = new JavaScriptSerializer();
            List<T> objs = Serializer.Deserialize<List<T>>(JsonStr);
            return objs;
        }
     
        public static T Deserialize<T>(string json)
        {
            T obj = Activator.CreateInstance<T>();
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
                return (T)serializer.ReadObject(ms);
            }
    }

    好了,我们来测试下

    string JsonStr = "[{Name:'苹果',Price:5.5},{Name:'橘子',Price:2.5},{Name:'柿子',Price:16}]";
    List<Product> products = new List<Product>();
    products = JSONStringToList<Product>(JsonStr);
    //Response.Write(products.Count());
    foreach (var item in products)
    {
       Response.Write(item.Name + ":" + item.Price + "<br />");
    }
    public class Product
    {
       public string Name { get; set; }
       public double Price { get; set; }
    }

    结果:

    苹果:5.5
    橘子
    :2.5
    柿子:16

     

    ==============================

    C#将对象序列化成JSON字符串

     

    using System.Web.Script.Serialization;
    public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
     
                List<Product> products = new List<Product>(){
                new Product(){Name="苹果",Price=5.5},
                new Product(){Name="橘子",Price=2.5},
    new Product(){Name="干柿子",Price=16.00}
                };
                ProductList productlist = new ProductList();
                productlist.GetProducts = products;
                context.Response.Write(new JavaScriptSerializer().Serialize(productlist));
            }
    public bool IsReusable
    {
    get
    {
                    return false;
    }
    }
     
            public class Product
            {
                public string Name { get; set; }
                public double Price { get; set; }
            }
     
            public class ProductList
            {
                public List<Product> GetProducts { get; set; }
            }
     

     

    生成的JSON结果如下:

    {"GetProducts":[{"Name":"苹果","Price":5.5},{"Name":"橘子","Price":2.5},{"Name":"柿子","Price":16}]}

     

    来源:
    http://www.rczjp.cn/HTML/110126/20110226120213.html
    http://www.rczjp.cn/HTML/101201/20104701014751.html

  • 相关阅读:
    Qt初始化窗口大小
    Ignatius's puzzle(数学)
    无法定位程序输入点_Z5qFreePv于动态链接库QtCore4.dll
    Harmonic Number(打表法)
    Codeforces Round #170 (Div. 2) C. Learning Languages(并查集)
    Codeforces Round #174 (Div. 2) B. Cows and Poker Game(简单)
    Codeforces Round #175 (Div. 2) B. Find Marble(简单模拟)
    错误:no matching function for call to 'Ui_***::retranslateUi()'
    最大流EK算法模板(BFS实现)
    Shaking Your Cellphone(并查集)
  • 原文地址:https://www.cnblogs.com/3Tai/p/json.html
Copyright © 2011-2022 走看看