JsonHelper(C#版)
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace WindowsFormsApp6
{
public class JSONHelper
{
public static bool UpdateJSON(string key, JObject data, string jsonfilePath)
{
if (!File.Exists(jsonfilePath))
{
JObject j1 = new JObject();
j1[key] = data;
string json = JsonConvert.SerializeObject(j1);
File.WriteAllText(jsonfilePath, json);
return true;
}
string jsonString = "";
using (StreamReader reader = new StreamReader(jsonfilePath))
{
jsonString = reader.ReadToEnd();
}
JObject rawjsonObject = JObject.Parse(jsonString);
rawjsonObject[key] = data;
string appendjson = JsonConvert.SerializeObject(rawjsonObject);
File.WriteAllText(jsonfilePath, appendjson);
return true;
}
public static bool AppendStringJSON(string key, string content, string jsonfilePath)
{
if (!File.Exists(jsonfilePath))
{
JObject j1 = new JObject();
j1[key] = content;
string json = JsonConvert.SerializeObject(j1);
File.WriteAllText(jsonfilePath, json);
return true;
}
string jsonString = "";
using (StreamReader reader = new StreamReader(jsonfilePath))
{
jsonString = reader.ReadToEnd();
}
JObject rawjsonObject = JObject.Parse(jsonString);
rawjsonObject[key] = content;
string appendjson = JsonConvert.SerializeObject(rawjsonObject);
File.WriteAllText(jsonfilePath, appendjson);
return true;
}
public static bool AppendJSON(string key, JObject data, string jsonfilePath)
{
if (!File.Exists(jsonfilePath))
{
JObject j1 = new JObject();
j1[key] = data;
string json = JsonConvert.SerializeObject(j1);
File.WriteAllText(jsonfilePath, json);
return true;
}
string jsonString = "";
using (StreamReader reader = new StreamReader(jsonfilePath))
{
jsonString = reader.ReadToEnd();
}
JObject rawjsonObject = JObject.Parse(jsonString);
rawjsonObject[key] = data;
string appendjson = JsonConvert.SerializeObject(rawjsonObject);
File.WriteAllText(jsonfilePath, appendjson);
return true;
}
public static bool DeleteJSON(string key, string jsonfilePath)
{
if (!File.Exists(jsonfilePath))
{
return false;
}
string jsonString = "";
using (StreamReader reader = new StreamReader(jsonfilePath))
{
jsonString = reader.ReadToEnd();
}
JObject rawjsonObject = JObject.Parse(jsonString);
rawjsonObject.Remove(key);
string appendjson = JsonConvert.SerializeObject(rawjsonObject);
File.WriteAllText(jsonfilePath, appendjson);
return true;
}
public static bool WriteJSON(object data, string file)
{
string json = JsonConvert.SerializeObject(data);
File.WriteAllText(file, json);
return true;
}
public static JObject ReadJSON(string jsonfilePath)
{
try
{
if (!File.Exists(jsonfilePath))
{
JObject j1 = new JObject();
string json = JsonConvert.SerializeObject(j1);
File.WriteAllText(jsonfilePath, json);
}
string jsonString = "";
using (StreamReader reader = new StreamReader(jsonfilePath))
{
jsonString = reader.ReadToEnd();
}
JObject jsonObject = JObject.Parse(jsonString);
return jsonObject;
}
catch (Exception e)
{
JObject jsonObject = null;
return jsonObject;
}
}
}
}
基本使用
JSONHelper.AppendJSON
JObject data = new JObject();
data["job"] = this.tbx_Job.Text.Trim();
data["resDianWei"] = this.tbx_resDianWei.Text.Trim();
data["rawDianWei"] = this.tbx_rawDianWei.Text.Trim();
string key = this.btnName;
string jsonfilePath = "DianWei.json";
JSONHelper.AppendJSON(key, data, jsonfilePath);
JSONHelper.AppendStringJSON
string s1 = materialTextBoxEdit1.Text.Trim();
string s2 = materialTextBoxEdit2.Text.Trim();
string s3 = materialTextBoxEdit3.Text.Trim();
JSONHelper.AppendStringJSON("s1_path", s1, "param.json");
JSONHelper.AppendStringJSON("s2_path", s2, "param.json");
JSONHelper.AppendStringJSON("s3_path", s3, "param.json");
MessageBox.Show("保存参数成功!");
JSONHelper.ReadJSON
JObject para = JSONHelper.ReadJSON("./param.json");
materialTextBoxEdit1.Text = (string)para["s1_path"];
materialTextBoxEdit2.Text = (string)para["s2_path"];
materialTextBoxEdit3.Text = (string)para["s3_path"];