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

 

posted @ 2018-04-28 10:24  坐拥百态  阅读(129)  评论(0编辑  收藏  举报