public class Tool { ////Donet2.0 需要添加引用 // 从一个对象信息生成Json串 public static string ObjectToJson(object obj) { return JsonConvert.ToString(obj); } // 从一个Json串生成对象信息 public static object JsonToObject(string jsonString, object obj) { return JsonConvert.DeserializeObject(jsonString, obj.GetType()); } //string jsonText = "{"zone":"海淀","zone_en":"haidian"}"; //Tool jo = (Tool)JsonConvert.DeserializeObject(jsonText); //string zone = jo["zone"].ToString(); //string zone_en = jo["zone_en"].ToString(); //var sr = JsonConvert.DeserializeAnonymousType<Resu>(jsonText, new Resu()); public byte[] StringToByte(string value) { return Encoding.UTF8.GetBytes(value); //return Text.Encoding.Default.GetBytes (value); } public string ByteToString(byte[] arr) { return Encoding.UTF8.GetString(arr); //return Text.Encoding.UTF8.GetString (arr, 0, arr.Length); } /// <summary> /// 字典转json字符串 /// </summary> /// <param name="myDic"></param> /// <returns></returns> public static string DictionaryToJson(Dictionary<string, string> myDic) { string jsonStr = JsonConvert.SerializeObject(myDic); return jsonStr; } /// <summary> /// json转字典 /// </summary> /// <param name="jsonStr"></param> /// <returns></returns> public static Dictionary<string, string> JsonToDictionary(string jsonStr) { Dictionary<string, string> dic = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonStr); return dic; } #region Parse /// <summary> /// 参数解码 /// </summary> /// <param name="data">要解码的字节数组</param> /// <param name="length">从0位置开始解码的长度</param> /// <returns>解码结果</returns> public static Dictionary<string, string> ParamDecoding(byte[] data, int length) { string outs = Encoding.UTF8.GetString(data, 0, (int)length); Dictionary<string, string> pram = new Dictionary<string, string>(); string[] list = outs.Split("|".ToArray(), StringSplitOptions.RemoveEmptyEntries); foreach (string item in list) { string[] x = item.Split("=".ToArray(), StringSplitOptions.RemoveEmptyEntries); pram.Add(WidthDecoding(x[0]), WidthDecoding(x[1])); } return pram; } /// <summary> /// 参数编码 /// </summary> /// <param name="param">要编码的参数集合</param> /// <returns>编码的结果字节</returns> public static byte[] ParamEcoding(Dictionary<string, string> param) { StringBuilder bf = new StringBuilder(); foreach (string item in param.Keys) { bf.Append(WidthEcoding(item)); //bf.Append(item); bf.Append("="); bf.Append(WidthEcoding(param[item])); bf.Append("|"); //bf.Append(list[item]); } byte[] outs = Encoding.UTF8.GetBytes(bf.ToString()); return outs; } /// <summary> /// 款字节解码 /// </summary> /// <param name="p_Value">要解码的宽字符编码(如:u0efxu094f)</param> /// <returns></returns> public static string WidthDecoding(string p_Value) { //string _ReturnText = ""; StringBuilder _ReturnText = new StringBuilder(); string[] _ValueList = p_Value.Split(new char[] { '\', 'u' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i != _ValueList.Length; i++) { char _ValueChar = Convert.ToChar(Convert.ToUInt16(_ValueList)); _ReturnText.Append(_ValueChar.ToString()); } return _ReturnText.ToString(); } /// <summary> /// 宽字符编码 /// </summary> /// <param name="s">要编码的字符串</param> /// <returns>结果(如:u0efxu094f)</returns> public static string WidthEcoding(string s) { StringBuilder outs = new StringBuilder(); foreach (char item in s) { outs.Append(string.Format("}", (int)item)); } return outs.ToString(); } #endregion }
;