zoukankan      html  css  js  c++  java
  • Xml,Json序列化

    Json到对象:

      using System.Runtime.Serialization.Json;
    
      public static T JsonAndObject<T>(string Json)
      where T : new()
      {
        DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
        using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(Json)))
        {
          T jsonObject = (T)ser.ReadObject(ms);
          return jsonObject;
        }
    
      }
    调用方式: List<T> list = AnalyzeXml.JsonAndObject<List<T>>(Json); 
    JObject Jsons = (JObject)JsonConvert.DeserializeObject(Json, Type.GetType("System.Data.UTF-8"));
    调用方式:string JsonStr=Jsons["Key"].ToString();

    对象到Json:

    public static string ObjectToJson(object obj)
      {
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
        MemoryStream stream = new MemoryStream();
        serializer.WriteObject(stream, obj);
        byte[] dataBytes = new byte[stream.Length];
        stream.Position = 0;
        stream.Read(dataBytes, 0, (int)stream.Length);
        return Encoding.UTF8.GetString(dataBytes);
      }
    调用方式:String Json= ObjectToJson(T);

     Xml转对象:

      public static T Deserialize<T>(string xml) where T : new()
      {
        T t = new T();
      try
      {
      //xml = "<?xml version="1.0" encoding="utf-8" ?>" + xml;
        using (StringReader sr = new StringReader(xml))
        {
          XmlSerializer aa = new XmlSerializer(typeof(T));
          t = (T)aa.Deserialize(sr);
        }
        return t;
      }
      catch (Exception ex)
      {
        return new T();
      }
      }
    调用: T t=Deserialize<T>(Xml);

    对象到xml:

            public static string Serializer(Type type, object obj)
            {
                MemoryStream Stream = new MemoryStream();
                //创建序列化对象 
                XmlSerializer xml = new XmlSerializer(type);
                try
                {
                    //序列化对象 
                    xml.Serialize(Stream, obj);
                }
                catch (Exception ex)
                {
                    new LogManager().WriteLine("【对象转为Xml错误】:" + ex.Message + ";");
                    return "";
                }
                Stream.Position = 0;
                StreamReader sr = new StreamReader(Stream);
                string str = sr.ReadToEnd().Replace("<?xml version="1.0"?>", "").Replace(" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"", "");
                Regex regex = new Regex(@"[<]{1}[a-zA-Z]{1,30}[ ]{0,3}[/]{1}[>]{1}");
                while (regex.IsMatch(str))
                {
                    Regex regexNew = new Regex(@"[a-zA-Z]{0,20}");
                    MatchCollection matchColl = regex.Matches(str);
                    foreach (Match mc in matchColl)
                    {
                        MatchCollection match = regexNew.Matches(regex.Match(str).ToString());
                        if (mc.ToString() != "")
                        {
                            foreach (Match m in match)
                            {
                                if (m.ToString() != "")
                                {
                                    str = str.Replace(mc.ToString(), string.Format("<{0}></{0}>", m.ToString()));
                                    break;
                                }
                            }
                        }
                    }
    
                }
                return str;
            }
  • 相关阅读:
    年终盘点 | 七年零故障支撑 双11 的消息中间件 RocketMQ,怎么做到的?
    刚刚,阿里云知行动手实验室正式开放公测了
    dubbogo 3.0:牵手 gRPC 走向云原生时代
    一个改变世界的“箱子”
    我看技术人的成长路径
    云原生体系下的技海浮沉与理论探索
    分布式事务框架 seata-golang 通信模型详解
    Serverless 如何落地?揭秘阿里核心业务大规模落地实现
    Github 2020 年度报告:你以为新冠击溃了开发者?不!他们创造了更多代码...
    493. Reverse Pairs
  • 原文地址:https://www.cnblogs.com/BoyStyle/p/8966193.html
Copyright © 2011-2022 走看看