C# NEWTONSOFT.JSON更改json文件

public static void Update()
{
string jsonfile = Directory.GetCurrentDirectory() + "\\config.json";
if (File.Exists(jsonfile))
{
string jsonString = File.ReadAllText(jsonfile, Encoding.Default);//读取文件
JObject jobject = JObject.Parse(jsonString);//解析成json
jobject["Devices"]["name"] = "555555";//替换需要的文件
string convertString = Convert.ToString(jobject);//将json装换为string
File.WriteAllText(jsonfile, convertString);//将内容写进jon文件中
}
else
{

}
}

 

config.json文件内容如下:

{
"Name": "lotname",
"Address": "wenzhou",
"Devices": {
"id": "001111",
"name": "555555",
"ip": "192.168.1.1"
},
"Doors": "4"
}

posted on 2018-06-13 16:28  hbgjh  阅读(3066)  评论(0编辑  收藏  举报