JsonHelper(C#版)

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
    {

        /// <summary>
        /// 内容是JObject
        /// </summary>
        /// <param name="key"></param>
        /// <param name="data"></param>
        /// <param name="jsonfilePath"></param>
        /// <returns></returns>
        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
            JObject rawjsonObject = JObject.Parse(jsonString);
            // 添加键值
            rawjsonObject[key] = data;
            // 序列化
            string appendjson = JsonConvert.SerializeObject(rawjsonObject);
            // 保存json
            File.WriteAllText(jsonfilePath, appendjson);
            return true;
        }




        /// <summary>
        /// 内容是字符串
        /// </summary>
        /// <param name="key"></param>
        /// <param name="content"></param>
        /// <param name="jsonfilePath"></param>
        /// <returns></returns>
        public static bool AppendStringJSON(string key, string content, string jsonfilePath)
        {
            // 如果文件不存在,则创建
            if (!File.Exists(jsonfilePath))
            {
                JObject j1 = new JObject();
                // 转为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
            JObject rawjsonObject = JObject.Parse(jsonString);
            // 
            rawjsonObject[key] = content;

            string appendjson = JsonConvert.SerializeObject(rawjsonObject);
            // 保存json
            File.WriteAllText(jsonfilePath, appendjson);
            return true;

        }

        /// <summary>
        /// 内容是JObject
        /// </summary>
        /// <param name="key"></param>
        /// <param name="data"></param>
        /// <param name="jsonfilePath"></param>
        /// <returns></returns>
        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
            JObject rawjsonObject = JObject.Parse(jsonString);
            // 添加键值
            rawjsonObject[key] = data;
            // 序列化
            string appendjson = JsonConvert.SerializeObject(rawjsonObject);
            // 保存json
            File.WriteAllText(jsonfilePath, appendjson);
            return true;
        }


        public static bool DeleteJSON(string key, string jsonfilePath)
        {
            // 如果文件不存在,则返回false
            if (!File.Exists(jsonfilePath))
            {
                return false;
            }
            string jsonString = "";
            using (StreamReader reader = new StreamReader(jsonfilePath))
            {
                jsonString = reader.ReadToEnd();
            }
            // 字符串转JObject
            JObject rawjsonObject = JObject.Parse(jsonString);
            // 删除键值
            rawjsonObject.Remove(key);
            string appendjson = JsonConvert.SerializeObject(rawjsonObject);
            // 保存json
            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;
                //MessageBox.Show("Json文件不存在");
                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";
// 保存到指定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"];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值