C# 读写json文件

cpp用时间长了,换换新口味,写写C#。这是使用C#读写json文件的笔记。

创建新工程:

在这里插入图片描述

在这里插入图片描述

添加NuGet包:

在这里插入图片描述

下载安装:

在这里插入图片描述

程序包安装完成:

在这里插入图片描述

拖两个按钮出来:

在这里插入图片描述

重要的:

using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

添加Hunter类:

    public partial class Hunter
    {
        public string system;
        public List<string> arrCharacter = new List<string>();
    }

write:

        //write
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                // 创建对象 
                JObject jsObj = new JObject();
                jsObj.Add("career", "hunter");


                JObject jsSystem = new JObject();
                jsSystem.Add("name", "name");
                jsSystem.Add("count", 999);
                jsSystem.Add("unique", false);


                JArray jsArraySys0 = new JArray();

                JObject jsSys1 = new JObject();
                jsSys1.Add("system_name", "强化系");
                JArray jsArraySys1 = new JArray();
                jsArraySys1.Add(new JValue("Gon"));
                jsArraySys1.Add(new JValue("雷欧力"));
                jsArraySys1.Add(new JValue("窝金"));
                jsSys1.Add("character", jsArraySys1);
                jsArraySys0.Add(jsSys1);

                JObject jsSys2 = new JObject();
                jsSys2.Add("system_name", "变化系");
                JArray jsArraySys2 = new JArray();
                jsArraySys2.Add(new JValue("Killua"));
                jsArraySys2.Add(new JValue("西索"));
                jsSys2.Add("character", jsArraySys2);
                jsArraySys0.Add(jsSys2);

                JObject jsSys3 = new JObject();
                jsSys3.Add("system_name", "操作系");
                JArray jsArraySys3 = new JArray();
                jsArraySys3.Add(new JValue("侠客"));
                jsArraySys3.Add(new JValue("半藏"));
                jsSys3.Add("character", jsArraySys3);
                jsArraySys0.Add(jsSys3);

                JObject jsSys4 = new JObject();
                jsSys4.Add("system_name", "具现化系");
                JArray jsArraySys4 = new JArray();
                jsArraySys4.Add(new JValue("Kula"));
                jsArraySys4.Add(new JValue("库哔"));
                jsArraySys4.Add(new JValue("小嘀"));
                jsSys4.Add("character", jsArraySys4);
                jsArraySys0.Add(jsSys4);

                JObject jsSys5 = new JObject();
                jsSys5.Add("system_name", "放出系");
                JArray jsArraySys5 = new JArray();
                jsArraySys5.Add(new JValue("富兰克林"));
                jsArraySys5.Add(new JValue("旋律"));
                jsArraySys5.Add(new JValue("爆弹魔"));
                jsSys5.Add("character", jsArraySys5);
                jsArraySys0.Add(jsSys5);

                JObject jsSys6 = new JObject();
                jsSys6.Add("system_name", "特质系");
                JArray jsArraySys6 = new JArray();
                jsArraySys6.Add(new JValue("Kula-火红眼"));
                jsArraySys6.Add(new JValue("库洛洛"));
                jsArraySys6.Add(new JValue("派克诺妲"));
                jsSys6.Add("character", jsArraySys6);
                jsArraySys0.Add(jsSys6);


                jsSystem.Add("system", jsArraySys0);
                jsObj.Add("system", jsSystem);
                string strSrc = Convert.ToString(jsObj);//将json装换为string
                File.WriteAllText(Directory.GetCurrentDirectory() + "\\test.json", strSrc, System.Text.Encoding.UTF8);//将内容写进json文件
            }
            catch (Exception ex)
            {
            }
        }

写成功,打开看看:

在这里插入图片描述
在这里插入图片描述

read:

 //read
        private void button2_Click(object sender, EventArgs e)
        {

            string folder = Directory.GetCurrentDirectory(); //获取应用程序的当前工作目录。 
            string path = folder + "\\test.json";

            StreamReader file = File.OpenText(path);
            JsonTextReader reader = new JsonTextReader(file);
            JObject jsonObject = (JObject)JToken.ReadFrom(reader);

            string career = jsonObject["career"].ToString();

            int nCount = int.Parse(jsonObject["system"]["count"].ToString());

            bool unique = bool.Parse(jsonObject["system"]["unique"].ToString());


            if(m_arrHunter == null)
            {
                m_arrHunter = new List<Hunter>();
            }
            m_arrHunter.Clear();
            int nSize = jsonObject["system"]["system"].Count();
            for (int i = 0;i<nSize;i++)
            {
                Hunter hunter = new Hunter();
                hunter.system = jsonObject["system"]["system"][i]["system_name"].ToString();
                int nSize2 = jsonObject["system"]["system"][i]["character"].Count();
                for(int j = 0;j<nSize2;j++)
                {
                    hunter.arrCharacter.Add(jsonObject["system"]["system"][i]["character"][j].ToString());
                }
                m_arrHunter.Add(hunter);
            }

            
            for(int i = 0;i<m_arrHunter.Count;i++)
            {
                Console.Write(m_arrHunter[i].system+":\n");

                int nSize2 = jsonObject["system"]["system"][i]["character"].Count();
                for (int j = 0; j < nSize2; j++)
                {
                    Console.Write(jsonObject["system"]["system"][i]["character"][j].ToString() + "\t");
                }

                Console.WriteLine("\n");
            }
            int xxx = 12;
        }

表结构:

在这里插入图片描述

打印出来看看:

在这里插入图片描述

  • 3
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值