zoukankan      html  css  js  c++  java
  • C# 使用Newtonsoft.Json读写Json文件

    {
      "CAN": false,
      "AccCode": 4294901856,
      "Id": 768,
      "BPointMove": true,
      "L_BPointMoveDelay": "600",
      "R_BPointMoveDelay": "1000"
    }
    复制代码

    1.Read

    复制代码
    try
    {
                    using (StreamReader file = File.OpenText("config.json"))
    { using( JsonTextReader reader = new JsonTextReader(file))
    { JObject jsonObject = (JObject)JToken.ReadFrom(reader); CAN_Communication = (bool) jsonObject["CAN"]; AccCode = (uint) jsonObject["AccCode"]; Id = (uint) jsonObject["Id"]; // Configure Json BPointMove = (bool)jsonObject["BPointMove"]; _classLeft.DelayBPointMove = (int)jsonObject["L_BPointMoveDelay"]; _classRight.DelayBPointMove = (int)jsonObject["R_BPointMoveDelay"]; file.Close();
    } } } catch { //MessageBox.Show("CAN卡配置有误!"); }
    复制代码

    2.Write

    复制代码
    try 
                    {
                        string json = File.ReadAllText("config.json");
                        dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
                        jsonObj["L_BPointMoveDelay"] = LBPointdelay.ToString();
                        string output = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj, Newtonsoft.Json.Formatting.Indented);
                        File.WriteAllText("config.json", output);
                    }
                    catch { }
    复制代码
  • 相关阅读:
    华为设备SNMP配置
    Linux CP直接覆盖快速操作
    CentOS7快速安装PHP7.0指南
    Linux SSH免密码登录配置
    this的指向
    分页的模块layui
    multer中间件
    ajax实现图片上传
    MVC模式、加密、jsonwebtoken
    mongoose与express
  • 原文地址:https://www.cnblogs.com/net-sky/p/10339292.html
Copyright © 2011-2022 走看看