using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Runtime.Serialization; using System.Runtime.Serialization.Json; using System.Threading.Tasks; using System.Web.Script.Serialization; using System.Diagnostics; using Lskj.Push.Report; namespace Lskj.Push.Util { public class JsonTool { // 从一个对象信息生成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).Replace("\\",""); } // 从一个Json串生成对象信息 public static object JsonToObject(string jsonString, object obj) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType()); MemoryStream mStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)); return serializer.ReadObject(mStream); } // 从一个对象信息生成Json串 public static string DictionaryToJson(Dictionary dict) { StringBuilder json = new StringBuilder(); foreach (KeyValuePair pair in dict) { json.Append("\"").Append(pair.Key).Append("\"").Append(":").Append(ValueToJson(pair.Value)).Append(","); } //Console.WriteLine("json String ******"+json); if (json.Length > 0) { json.Remove(json.Length -1, 1); } json.Append("}"); json.Insert(0, "{"); return json.ToString(); } public static List JsonList(string jsonString) { JavaScriptSerializer Serializer = new JavaScriptSerializer(); List jsonclassList = Serializer.Deserialize>(jsonString); return jsonclassList; } //从dictionary 的value中解析出字符串 private static string ValueToJson(object value) { Type type = value.GetType(); if(type==typeof(int)){ return value.ToString(); }else if(type==typeof(string)){ return "\""+value+"\""; }else if(type==typeof(List)||type==typeof(List)){ return ObjectToJson(value); }else if(type==typeof(Dictionary)){ return JsonTool.DictionaryToJson((Dictionary)value); } else{ Debug.WriteLine("Type in Dictionary is error!"); return "type erro"; } } } }