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类。

posted @ 2020-03-02 15:06  风影我爱罗  阅读(5754)  评论(0编辑  收藏  举报