zoukankan      html  css  js  c++  java
  • 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类。

  • 相关阅读:
    CentOS7最小化安装后要做的事
    Django 2.0 新特性
    Django2.+ path配置
    Django 安装
    Django基础
    http协议
    Centos7 下mysql大小写敏感问题
    CentOS 设置mysql的远程访问
    Linux下彻底卸载mysql详解
    静态库和动态库
  • 原文地址:https://www.cnblogs.com/gaara-zhang/p/12395732.html
Copyright © 2011-2022 走看看