zoukankan      html  css  js  c++  java
  • MD5 哈希 和 JSON


     /// <summary> 

             /// 计算字符串的 MD5 哈希。若字符串为空,则返回空,否则返回计算结果。 

             /// </summary> 

             public static string ComputeMD5Hash( this string str ) 

             { 

                 string hash = str; 

       

                 if ( str != null ) 

                 { 

                     MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); 

                     byte[] data = Encoding.ASCII.GetBytes( str ); 

                     data = md5.ComputeHash( data ); 

                     hash = ""; 

                     for ( int i = 0; i < data.Length; i++ ) 

                         hash += data[ i ].ToString( "x2" ); 

                 } 

       

                 return hash; 

             } 

             /// <summary> 

             /// 获得 Json 描述的 .Net 对象 

             /// </summary> 

             public static T JsonToObject<T>( this string str ) 

             { 

                 return new JavaScriptSerializer().Deserialize<T>( str ); 

             } 

       

       

     //object extensions 

     /// <summary> 

             /// 获得 obj 的 JSON 字符串 

             /// </summary> 

             public static string ToJson( this object obj ) 

             { 

                 return new JavaScriptSerializer().Serialize( obj ); 

             } 

    //sample 

       

     public class Student 

     { 

         public string Name { get; set; } 

         public int Age { get; set; } 

     } 

       

     Student std = new Student() { Name = "xxxx", Age = 24 }; 

     std.ToJson();   //   =>  "\{ \"Name\": \"xxxx\", \"Age\": 24 \}" 

      

     string json = "\{ \"Name\": \"xxxx\", \"Age\": 24 \}"; 

     Student s = json.JsonToObject<Student>();

  • 相关阅读:
    波特率原理【转】
    求助大神!怎样批量删除数据库表中某个字段中同样的一段字符!
    1033. To Fill or Not to Fill (25)
    http协议
    【数据结构与算法】二叉树深度遍历(递归)
    2015届求职经历
    Codeforces Round #245 (Div. 1)——Working out
    泛泰A900 刷4.4专用中文TWRP2.7.1.1版 支持自己主动识别手机版本号(全球首创)
    实现简答LinkedList
    oracle的内存管理(之中的一个)
  • 原文地址:https://www.cnblogs.com/ihada/p/1958388.html
Copyright © 2011-2022 走看看