C#, .Net Core读写json、修改和删除Json节点

2020-08-28  添加单节点 删除,修改,读取功能,修复一些异常
2019-10-17 更新

JsonFileHelper.cs 内容
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;

namespace JsonTest
{//原创来自:http://www.luofenming.com/show.aspx?id=ART2019091500001
    class JsonHelper
    {
        private string _path;//根目录的相对途径

        /// <summary>
        /// 
        /// </summary>
        /// <param name="jsonName">根目录的相对途径包含文件名</param>
        public JsonHelper(string jsonName)
        {
            if (!jsonName.EndsWith(".json"))
            {
                _path = $"{jsonName}.json";
            }
            else
            {
                _path = jsonName;
            }
            if (!File.Exists(_path))
            {//不存在 创建一个json文件
                File.WriteAllText(_path, "{}");
            }
        }

        /// <summary>
        /// 读取Json返回实体对象
        /// </summary>
        /// <returns></returns>
        public T Read<T>() => Read<T>("");

        /// <summary>
        /// 根据节点读取Json返回实体对象
        /// </summary>
        /// <param name="section">根节点</param>
        /// <returns></returns>
        public T Read<T>(string section)
        {
            try
            {
                using (StreamReader file = new StreamReader(_path))
                using (JsonTextReader reader = new JsonTextReader(file))
                {
                    JToken secJt = JToken.ReadFrom(reader);
                    if (!string.IsNullOrWhiteSpace(section))
                    {//jObj[section].SelectToken("address").SelectToken("name")
                        string[] nodes = section.Split(':');
                        foreach (string node in nodes)
                        {
                            secJt = secJt[node];
                        }
                        if (secJt != null)
                        {
                            return JsonConvert.DeserializeObject<T>(secJt.ToString());
                        }
                    }
                    else
                    {
                        return JsonConvert.DeserializeObject<T>(secJt.ToString());
                    }
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return default(T);
        }

        /// <summary>
        /// 读取Json返回集合
        /// </summary>
        /// <returns></returns>
        public List<T> ReadList<T>() => ReadList<T>("");

        /// <summary>
        /// 根据节点读取Json返回集合
        /// </summary>
        /// <param name="section">根节点</param>
        /// <returns></returns>
        public List<T> ReadList<T>(string section)
        {
            try
            {
                using (StreamReader file = new StreamReader(_path))
                {
                    using (JsonTextReader reader = new JsonTextReader(file))
                    {
                        JToken secJt = JToken.ReadFrom(reader);
                        if (!string.IsNullOrWhiteSpace(section))
                        {
                            string[] nodes = section.Split(':');
                            foreach (string node in nodes)
                            {
                                secJt = secJt[node];
                            }
                            if (secJt != null)
                            {
                                return JsonConvert.DeserializeObject<List<T>>(secJt.ToString());
                            }
                        }
                        else
                        {
                            return JsonConvert.DeserializeObject<List<T>>(section.ToString());
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return default(List<T>);
        }

        /// <summary>
        /// 写入文件
        /// </summary>
        /// <typeparam name="T">自定义对象</typeparam>
        /// <param name="t"></param>
        public void Write<T>(T t) => Write("", t);

        /// <summary>
        /// 写入指定section文件
        /// </summary>
        /// <typeparam name="T">自定义对象</typeparam>
        /// <param name="t"></param>
        public void Write<T>(string section, T t)
        {
            try
            {
                JObject jObj;
                string json = JsonConvert.SerializeObject(t);
                if (string.IsNullOrWhiteSpace(section))
                {
                    jObj = JObject.Parse(json);
                }
                else
                {
                    using (StreamReader file = new StreamReader(_path))
                    using (JsonTextReader reader = new JsonTextReader(file))
                    {
                        try
                        {
                            jObj = (JObject)JToken.ReadFrom(reader);
                            jObj[section] = JObject.Parse(json);
                        }
                        catch (Exception ex)
                        {
                            jObj = JObject.Parse(json);
                        }
                    }
                }
                using (StreamWriter writer = new StreamWriter(_path))
                {
                    using (JsonTextWriter jsonWriter = new JsonTextWriter(writer))
                    {
                        jObj.WriteTo(jsonWriter);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 删除或修改 单个节点
        /// </summary>
        /// <param name="section">节点途径 格式为A:B:C  如果节点带数组格式  A:[0]:C 数组用中括号</param>
        /// <param name="isRemove">是删除还是修改</param>
        /// <param name="value">修改节点的内容</param>
        /// <param name="isNum">修改内容类型 数据:true  字符串:false</param>
        private void JTokenHander(string section, bool isRemove, string value, bool isNum)
        {
            try
            {
                JToken jObj;
                using (StreamReader file = new StreamReader(_path))
                {
                    using (JsonTextReader reader = new JsonTextReader(file))
                    {
                        jObj = JToken.ReadFrom(reader);
                        string[] nodes = section.Split(':');
                        JToken tempToken = jObj;
                        if (nodes != null && nodes.Length > 0)
                        {
                            foreach (string node in nodes)
                            {
                                if (node != null)
                                {
                                    if (node.StartsWith("[") && node.EndsWith("]"))
                                    {
                                        int tempi = 0;
                                        if (int.TryParse(node.Trim('[').Trim(']'), out tempi))
                                        {
                                            tempToken = tempToken[tempi];
                                        }
                                    }
                                    else
                                    {
                                        tempToken = tempToken[node];
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                        if (tempToken != null)
                        {
                            if (isRemove)
                            {
                                tempToken.Parent.Remove();
                            }
                            else
                            {
                                JToken token;
                                if (isNum)
                                {
                                    if (value.Contains("."))
                                    {
                                        double tempd;
                                        double.TryParse(value, out tempd);
                                        token = tempd;
                                    }
                                    else
                                    {
                                        int tempi = 0;
                                        int.TryParse(value, out tempi);
                                        token = tempi;
                                    }
                                    
                                }
                                else
                                {
                                    token = value;
                                }
                                tempToken.Replace(token);//改
                            }
                        }
                    }
                }
                using (StreamWriter writer = new StreamWriter(_path))
                {
                    using (JsonTextWriter jsonWriter = new JsonTextWriter(writer))
                    {
                        jObj.WriteTo(jsonWriter);
                    }
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 删除指定节点
        /// </summary>
        /// <param name="section">节点途径 格式为A:B:C  如果节点带数组格式  A:[0]:C 数组用中括号</param>
        public void Remove(string section)
        {
            JTokenHander(section, true, "", false);
        }
        /// <summary>
        /// 修改指定节点内容
        /// </summary>
        /// <param name="section">节点途径 格式为A:B:C  如果节点带数组格式  A:[0]:C 数组用中括号</param>
        /// <param name="value">修改节点的内容</param>
        /// <param name="isNum">修改内容类型 数据:true  字符串:false</param>
        public void Update(string section, string value, bool isNum)
        {
            JTokenHander(section, false, value, isNum);
        }
        /// <summary>
        /// 读取节点内容
        /// </summary>
        /// <param name="section">节点途径 格式为A:B:C  如果节点带数组格式  A:[0]:C 数组用中括号</param>
        /// <returns></returns>
        public string Read(string section)
        {
            try
            {
                JToken jObj;
                using (StreamReader file = new StreamReader(_path))
                {
                    using (JsonTextReader reader = new JsonTextReader(file))
                    {
                        jObj = JToken.ReadFrom(reader);
                        string[] nodes = section.Split(':');
                        JToken tempToken = jObj;
                        if (nodes != null && nodes.Length > 0)
                        {
                            foreach (string node in nodes)
                            {
                                if (node != null)
                                {
                                    if (node.StartsWith("[") && node.EndsWith("]"))
                                    {
                                        int tempi = 0;
                                        if (int.TryParse(node, out tempi))
                                        {
                                            tempToken = tempToken[tempi];
                                        }
                                    }
                                    else
                                    {
                                        tempToken = tempToken[node];
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                        if (tempToken != null)
                        {
                            return tempToken.ToString();
                        }
                    }
                }
              
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return "";
        }
    }
}

Model实体类
public class DataModel
{
    public int errorcode { get; set; }
    public string message { get; set; }
    public Person person { get; set; }
}
public class Person
{
    public int id { get; set; }
    public string name { get; set; }
    public string age { get; set; }
    public string sex { get; set; }
    public Address address { get; set; }
}
public class Address
{
    public string name { get; set; }
    public string postcode { get; set; }
}

JsonDemo.json 内容
{
  "errorcode": 0,
  "message": "lqwvje",
  "person": {
    "id": 1,
    "name": "罗分明",
    "age": 18,
    "sex": "男",
    "address": {
      "name": "江西九江",
      "postcode": "332600"
    },
    "listData": [ "ad", "asdf", "23" ]
  }
}

方法调用 注意如果读取不到,看生成的根目录有没有JsonDemo.json 这个文件 并且有上面Json内容
static void Test()
{
    CoreClassLibrary.JsonFileHelper jsonFileHelper = new CoreClassLibrary.JsonFileHelper("JsonDemo.json");
    //Address person = jsonFileHelper.Read<Address>("person:address");//获取json实内容

    #region 修改json
    //DataModel dataModel = jsonFileHelper.Read<DataModel>();//获取Json实体内容
    //dataModel.person.name = "luofenming";//修改实体内容
    //jsonFileHelper.Write<DataModel>(dataModel);//写入实体内容
    #endregion

    //string s = jsonFileHelper.ReadSingleNode("person:id");//查询单个节点值
    //string s2 = jsonFileHelper.ReadSingleNode("person:address:postcode");//查询单个节点值
    //List<string> list = jsonFileHelper.ReadList<string>("person:listData");//查询数组
    //jsonFileHelper.Remove("person:address");//删除节点
}

评论