c# 读写json文件
附代码:
public static void readWriteJson() { try { string path = Application.StartupPath + @"\data.json"; StreamReader streamReader = new StreamReader(path); string jsonStr = streamReader.ReadToEnd(); dynamic jsonObj = JsonConvert.DeserializeObject<dynamic>(jsonStr); jsonObj["userInfo"]["customerName"] = "123456"; streamReader.Close(); string output = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj, Newtonsoft.Json.Formatting.Indented); File.WriteAllText(path, output); } catch (Exception e) { MessageBox.Show(e.Message + "/r/n" + e.StackTrace); } }
附对应的json文件:
{ "userInfo": { "customerName": "123456", "sex": "男", "age": "49", "time": "2017-01-18 10:30 巳时", "quality": "86.5", "siteName": "医疗机构/健康会所" }, "thermalStructure": { "valRen": 3, "valDu": 2, "tripleEnergizer": [ -0.5, 3, 1.5 ], "viscera": [ -0.5, 3, 1, 2.5, 3, 2, 2, 1.5, 3, 2.5, 3.2, 3, 0.5, 0, 1, 1.5 ] }, "date": "2020-01-02" }
注意:
1.数组类型的赋值 借用 JArray类。