Json.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using System.Text;
  3. using System.IO;
  4. using System.Windows.Forms;
  5. using System.Collections;
  6. using Newtonsoft.Json;
  7. using Newtonsoft.Json.Linq;
  8. using Newtonsoft.Json.Converters;
  9. namespace BaseLibRWFile
  10. {
  11. //需根据实际要求编写
  12. public class LotModel
  13. {
  14. public string Name { get; set; }
  15. public string Address { get; set; }
  16. public Hashtable Devices { get; set; }
  17. public string Doors { get; set; }
  18. }
  19. public class ClsJson
  20. {
  21. public void Write(string jsonfile, string MainContent, string ChildContent, string ChildValue)
  22. {
  23. try
  24. {
  25. string[] MainContects = MainContent.Split(',');
  26. string[] ChildContects = ChildContent.Split(',');
  27. string[] ChildValues = ChildValue.Split(',');
  28. //子集
  29. Hashtable device = new Hashtable();
  30. //device.Add("www", ChildValues);
  31. if (ChildContects.Length == ChildValues.Length)
  32. {
  33. for (int i = 0; i < ChildContects.Length; i++)
  34. {
  35. device.Add(ChildContects[i], ChildValues[i]);
  36. }
  37. }
  38. LotModel lot = new LotModel
  39. {
  40. Name = MainContects[0],
  41. Address = MainContects[1],
  42. Devices = device, //注意是子集
  43. Doors = MainContects[3]
  44. }; //实体模型类
  45. //序列化
  46. string js1 = JsonConvert.SerializeObject(lot);
  47. string js2 = JsonConvert.SerializeObject(device);
  48. //反序列化
  49. LotModel debc1 = JsonConvert.DeserializeObject<LotModel>(js1);
  50. LotModel debc2 = JsonConvert.DeserializeObject<LotModel>(js2);
  51. //找到服务器物理路径
  52. //string serverAppPath = Request.PhysicalApplicationPath.ToString();
  53. if (!File.Exists(jsonfile))
  54. {
  55. FileStream fs1 = new FileStream(jsonfile, FileMode.Create, FileAccess.ReadWrite);
  56. fs1.Close();
  57. //File.Create(jsonfile);
  58. }
  59. //把模型数据写到文件
  60. using (StreamWriter sw = new StreamWriter(jsonfile))
  61. {
  62. JsonSerializer serializer = new JsonSerializer();
  63. serializer.Converters.Add(new JavaScriptDateTimeConverter());
  64. serializer.NullValueHandling = NullValueHandling.Ignore;
  65. //构建Json.net的写入流
  66. JsonWriter writer = new JsonTextWriter(sw);
  67. //把模型数据序列化并写入Json.net的JsonWriter流中
  68. serializer.Serialize(writer, lot);
  69. //ser.Serialize(writer, ht);
  70. writer.Close();
  71. sw.Close();
  72. }
  73. }
  74. catch (Exception ex)
  75. {
  76. MessageBox.Show("Write Json Error:" + ex.ToString());
  77. }
  78. }
  79. public void Read(string jsonfile, ref string MainContent, ref string ChildValue)
  80. {
  81. try
  82. {
  83. if (!File.Exists(jsonfile))
  84. {
  85. FileStream fs1 = new FileStream(jsonfile, FileMode.Create, FileAccess.ReadWrite);
  86. fs1.Close();
  87. }
  88. using (StreamReader file = File.OpenText(jsonfile))
  89. {
  90. using (JsonTextReader reader = new JsonTextReader(file))
  91. {
  92. JObject o = (JObject)JToken.ReadFrom(reader);
  93. string a = o["Name"].ToString();
  94. var b = o["Address"];
  95. JToken c = o["Devices"];
  96. var d = o["Doors"];
  97. MainContent = a.ToString() + "," + b.ToString() + "," + c.ToString() + "," + d.ToString();
  98. var deviceID = c["id"].ToString();
  99. var name = c["name"].ToString();
  100. var IP = c["ip"].ToString();
  101. ChildValue = deviceID.ToString() + "," + name.ToString() + "," + IP.ToString();
  102. }
  103. }
  104. }
  105. catch (Exception ex)
  106. {
  107. MessageBox.Show("Read Json Error:" + ex.ToString());
  108. }
  109. }
  110. public void Update(string jsonfile, string MainContent, string ChildContent, string ChildValue)
  111. {
  112. try
  113. {
  114. //jsonfile = jsonfile + "\\" + jsonname;//"D://config.json"; //Directory.GetCurrentDirectory() + "\\config.json";
  115. if (File.Exists(jsonfile))
  116. {
  117. string jsonString = File.ReadAllText(jsonfile, Encoding.Default);//读取文件
  118. JObject jobject = JObject.Parse(jsonString);//解析成json
  119. if (!string.IsNullOrEmpty(ChildContent))
  120. {
  121. jobject[MainContent][ChildContent] = ChildValue;//替换需要的文件
  122. }
  123. else
  124. {
  125. jobject[MainContent] = ChildValue;//替换需要的文件
  126. }
  127. string convertString = Convert.ToString(jobject);//将json装换为string
  128. File.WriteAllText(jsonfile, convertString);//将内容写进jon文件中
  129. }
  130. else
  131. {
  132. FileStream fs1 = new FileStream(jsonfile, FileMode.Create, FileAccess.ReadWrite);
  133. fs1.Close();
  134. }
  135. }
  136. catch (Exception ex)
  137. {
  138. MessageBox.Show("Updata Json Error:" + ex.ToString());
  139. }
  140. }
  141. }
  142. }