using System.Collections.Generic; using System.Data; using System.Web.Script.Serialization; namespace MESWebSite.Tool { public class JSONTools { public static List JSONStringToList(string JsonStr) { JavaScriptSerializer serializer = new JavaScriptSerializer(); serializer.MaxJsonLength = 0x7fffffff; return serializer.Deserialize>(JsonStr); } public static DataSet JsonToDataSet(string Json) { try { DataSet set = new DataSet(); JavaScriptSerializer serializer = new JavaScriptSerializer(); serializer.MaxJsonLength = 0x7fffffff; Dictionary dictionary = (Dictionary)serializer.DeserializeObject(Json); foreach (KeyValuePair pair in dictionary) { DataTable table = new DataTable(pair.Key); object[] objArray = (object[])pair.Value; foreach (object obj3 in objArray) { Dictionary dictionary2 = (Dictionary)obj3; DataRow row = table.NewRow(); foreach (KeyValuePair pair2 in dictionary2) { if (!table.Columns.Contains(pair2.Key)) { table.Columns.Add(pair2.Key.ToString()); row[pair2.Key] = pair2.Value; } else { row[pair2.Key] = pair2.Value; } } table.Rows.Add(row); } set.Tables.Add(table); } return set; } catch { return null; } } public static string ModifyInfoJSON(string people, string date) { return ("{\"People\":\"" + people + "\",\"Date\":\"" + date + "\"}"); } public static T ScriptDeserialize(string json) where T : new() { T local = new T(); JavaScriptSerializer serializer = new JavaScriptSerializer(); serializer.MaxJsonLength = 0x7fffffff; return serializer.Deserialize(json); } public static string ScriptSerialize(T input) { JavaScriptSerializer serializer = new JavaScriptSerializer(); serializer.MaxJsonLength = 0x7fffffff; return serializer.Serialize(input); } public static string ScriptSerialize(object input) { return Newtonsoft.Json.JsonConvert.SerializeObject(input); } public static int StringToInt(string str) { int result = 0; if (string.IsNullOrEmpty(str)) { return result; } if (int.TryParse(str.Trim(), out result)) { return result; } return result; } } }