using System; using System.Text; using System.IO; using System.Windows.Forms; using System.Collections; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Converters; namespace BaseLibRWFile { //需根据实际要求编写 public class LotModel { public string Name { get; set; } public string Address { get; set; } public Hashtable Devices { get; set; } public string Doors { get; set; } } public class ClsJson { public void Write(string jsonfile, string MainContent, string ChildContent, string ChildValue) { try { string[] MainContects = MainContent.Split(','); string[] ChildContects = ChildContent.Split(','); string[] ChildValues = ChildValue.Split(','); //子集 Hashtable device = new Hashtable(); //device.Add("www", ChildValues); if (ChildContects.Length == ChildValues.Length) { for (int i = 0; i < ChildContects.Length; i++) { device.Add(ChildContects[i], ChildValues[i]); } } LotModel lot = new LotModel { Name = MainContects[0], Address = MainContects[1], Devices = device, //注意是子集 Doors = MainContects[3] }; //实体模型类 //序列化 string js1 = JsonConvert.SerializeObject(lot); string js2 = JsonConvert.SerializeObject(device); //反序列化 LotModel debc1 = JsonConvert.DeserializeObject(js1); LotModel debc2 = JsonConvert.DeserializeObject(js2); //找到服务器物理路径 //string serverAppPath = Request.PhysicalApplicationPath.ToString(); if (!File.Exists(jsonfile)) { FileStream fs1 = new FileStream(jsonfile, FileMode.Create, FileAccess.ReadWrite); fs1.Close(); //File.Create(jsonfile); } //把模型数据写到文件 using (StreamWriter sw = new StreamWriter(jsonfile)) { JsonSerializer serializer = new JsonSerializer(); serializer.Converters.Add(new JavaScriptDateTimeConverter()); serializer.NullValueHandling = NullValueHandling.Ignore; //构建Json.net的写入流 JsonWriter writer = new JsonTextWriter(sw); //把模型数据序列化并写入Json.net的JsonWriter流中 serializer.Serialize(writer, lot); //ser.Serialize(writer, ht); writer.Close(); sw.Close(); } } catch (Exception ex) { MessageBox.Show("Write Json Error:" + ex.ToString()); } } public void Read(string jsonfile, ref string MainContent, ref string ChildValue) { try { if (!File.Exists(jsonfile)) { FileStream fs1 = new FileStream(jsonfile, FileMode.Create, FileAccess.ReadWrite); fs1.Close(); } using (StreamReader file = File.OpenText(jsonfile)) { using (JsonTextReader reader = new JsonTextReader(file)) { JObject o = (JObject)JToken.ReadFrom(reader); string a = o["Name"].ToString(); var b = o["Address"]; JToken c = o["Devices"]; var d = o["Doors"]; MainContent = a.ToString() + "," + b.ToString() + "," + c.ToString() + "," + d.ToString(); var deviceID = c["id"].ToString(); var name = c["name"].ToString(); var IP = c["ip"].ToString(); ChildValue = deviceID.ToString() + "," + name.ToString() + "," + IP.ToString(); } } } catch (Exception ex) { MessageBox.Show("Read Json Error:" + ex.ToString()); } } public void Update(string jsonfile, string MainContent, string ChildContent, string ChildValue) { try { //jsonfile = jsonfile + "\\" + jsonname;//"D://config.json"; //Directory.GetCurrentDirectory() + "\\config.json"; if (File.Exists(jsonfile)) { string jsonString = File.ReadAllText(jsonfile, Encoding.Default);//读取文件 JObject jobject = JObject.Parse(jsonString);//解析成json if (!string.IsNullOrEmpty(ChildContent)) { jobject[MainContent][ChildContent] = ChildValue;//替换需要的文件 } else { jobject[MainContent] = ChildValue;//替换需要的文件 } string convertString = Convert.ToString(jobject);//将json装换为string File.WriteAllText(jsonfile, convertString);//将内容写进jon文件中 } else { FileStream fs1 = new FileStream(jsonfile, FileMode.Create, FileAccess.ReadWrite); fs1.Close(); } } catch (Exception ex) { MessageBox.Show("Updata Json Error:" + ex.ToString()); } } } }