C# serialization and deserialization conversion of several formats

The first is to convert object to string object, which is relatively simple and nothing to talk about;

 public string ScriptSerialize<T>(T t)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(t);
        }


The second converts object to xml object:

copy code
 public string ScriptSerializeToXML<T>(T t)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            MemoryStream mem = new MemoryStream();
            XmlTextWriter writer = new XmlTextWriter(mem,Encoding.UTF8);
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("","");
            serializer.Serialize(writer,t,ns);
            writer.Close();
            return Encoding.UTF8.GetString(mem.ToArray());
        }
copy code

Below I mainly talk about the deserialization of string objects into corresponding objects;

1. Deserialize the string object into an object object

 public T ScriptDeserialize<T>(string strJson)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Deserialize<T>(strJson);
        }

2. Deserialize the string object into a list object

 public List<T> JSONStringToList<T>(string strJson)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            List<T> objList = serializer.Deserialize<List<T>>(strJson);
            return objList;
        }

3. Deserialize the string object into a datatable object

copy code
  public DataTable JSONStringToDataTable<T>(string strJson)
        {
            DataTable dt = new DataTable();
             if (strJson.IndexOf( " [ " ) > - 1 ) // If greater than, strJson stores multiple model objects 
            {
                strJson = strJson.Remove(strJson.Length - 1, 1).Remove(0, 1).Replace("},{
    
    ", "};{
    
    ");
            }
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string[] items = strJson.Split(';');

            foreach (PropertyInfo property in  typeof (T).GetProperties()) // Get all properties of type T through reflection 
            {
                DataColumn col = new DataColumn(property.Name,property.PropertyType);
                dt.Columns.Add(col);
            }
            // loop one by one deserialize 
            for ( int i = 0 ; i < items.Length; i++ )
            {
                DataRow dr = dt.NewRow();
                 // Deserialize to a T type object 
                T temp = serializer.Deserialize<T> (items[i]);
                 foreach (PropertyInfo property in  typeof (T).GetProperties())
                {
                    dr[property.Name] = property.GetValue(temp,null);
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }
copy code

Fourth, deserialize the xml object into an object object

copy code
 public T JSONXMLToObject<T>(string strJson)
        {
            XmlDocument xdoc = new XmlDocument();
            try
            {
                xdoc.LoadXml (strJson);
                XmlNodeReader reader = new XmlNodeReader(xdoc.DocumentElement);
                XmlSerializer ser = new XmlSerializer(typeof(T));
                object obj = ser.Deserialize(reader);
                return (T)obj;
            }
            catch
            {
                return default(T);
            }
        }
copy code

Now how to call them with specific instances? Pay special attention to the deserialization of xml objects to objcet objects

 public class LoginObject
    {
          public string Account { get; set;}
          public string Password { get; set;}
     }

 

copy code
1   LoginObject loginObject = new LoginObject { Account = account, Password = password };
 2              ExTools.Manage.Class.CScriptSerialize Serialize = new Class.CScriptSerialize();
 3              // Convert object to string 
4               string strJson= Serialize.ScriptSerialize( loginObject);
 5            
6              // Convert object to xml object 
7              string strJson = Serialize.ScriptSerializeToXML(loginObject);
 8             
9  
10              // Convert to list object 
11               List<LoginObject> list = Serialize.JSONStringToList<LoginObject>(strJson);
 12              // Convert an xml object to an object object 
13             strJson = strJson.Substring( 1 , strJson.Length - 1 );
 14              loginObject = Serialize.JSONXMLToObject<LoginObject> (strJson);
 15              // Convert the string Convert to dataTable 
16              DataTable dt = Serialize.JSONStringToDataTable<LoginObject> (strJson);
 17              // Convert string to object 
18              loginObject = Serialize.ScriptDeserialize<LoginObject>(strJson);
copy code

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324184767&siteId=291194637