zoukankan      html  css  js  c++  java
  • asp.net json Serialization

    1.必须应用System.serviceModel.Web;

    2.应用System.Runtime.Serialization.Json;System.Runtime.Serialization;

    3.coding

            /// <summary>
            /// 序列化
            /// </summary>
            /// <typeparam name="Entity"></typeparam>
            /// <param name="entity"></param>
            /// <returns></returns>
            public static string SerializeObject<Entity>(Entity entity)
            {

                DataContractJsonSerializer serializer = new DataContractJsonSerializer(entity.GetType());
                using (MemoryStream ms = new MemoryStream())
                {
                    serializer.WriteObject(ms, entity);
                    StringBuilder sb = new StringBuilder();
                    sb.Append(Encoding.UTF8.GetString(ms.ToArray()));
                    ms.Close();
                    return sb.ToString();
                }
            }


            public static string SerializeObject<Entity>(IList<Entity> entityList)
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(entityList.GetType());
                using (MemoryStream ms = new MemoryStream())
                {
                    serializer.WriteObject(ms, entityList);
                    StringBuilder sb = new StringBuilder();
                    sb.Append(Encoding.UTF8.GetString(ms.ToArray()));
                    ms.Close();
                    return sb.ToString();
                }
            }

            /// <summary>
            /// 反序列化
            /// </summary>
            /// <typeparam name="Entity"></typeparam>
            /// <param name="jsonString"></param>
            /// <returns></returns>
            public static Entity DeSerilizeObject<Entity>(string jsonString)
            {
                DataContractJsonSerializer deserialize = new DataContractJsonSerializer(typeof(Entity));
                using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
                {
                    Entity entity = (Entity)deserialize.ReadObject(ms);
                    ms.Close();
                    return entity;
                }
            }

  • 相关阅读:
    在 ML2 中 enable local network
    实践 Neutron 前的两个准备工作
    配置 linux-bridge mechanism driver
    为 Neutron 准备物理基础设施(II)
    两张图总结 Neutron 架构
    Service Plugin / Agent
    详解 ML2 Core Plugin(I)
    #define用法集锦[修正版]
    socketpair理解
    判断单链表是否存在环,判断两个链表是否相交-的相关讨论
  • 原文地址:https://www.cnblogs.com/xinlang/p/1751058.html
Copyright © 2011-2022 走看看