用c# 对Json文件进行增加,修改删除操作

假如有这样一个JSon文件  

{

    "school":
    {
        "class": [
        {
            "id": "高一",
            "name": "2班",
                "teacher":
                {
                    "yu": "张三",
                   "sx": "李四",
                   "yy": "王五"
                }

        }
        ]  
    }
}

 1.新加一个班级

string JsonPath = "D:\\school.json";
            string JsonString = File.ReadAllText(JsonPath, Encoding.UTF8);
            JObject jobject = JObject.Parse(JsonString);
            JObject newStu = new JObject(
                new JProperty("id", 2),
                new JProperty("name", "3班"),
                new JProperty("teacher", new JObject(
                                  new JProperty("yu", "qwe "),
                                  new JProperty("sx", "qwe"),
                                  new JProperty("yy", "qwe")
                )
                         )
                );
            jobject["school"]["class"].Last.AddAfterSelf(newStu);
            string convertString = Convert.ToString(jobject);
            File.WriteAllText(JsonPath, convertString);

2.删除掉某个班级

jobject["school"]["class"][Index].Remove();

3.修改某个班级的id

jobject["school"]["class"][index]["id"] = "123";

 


                 
注:需要引入Newtonsoft.Json。

 

首先,需要将json文件读入到程序中,可以使用Newtonsoft.Json库实现。然后,将json数据绑定到DataGridView控件上面,以实现数据的展示和编辑。对于增删改查操作,可以通过DataGridView控件提供的事件和方法来实现。 下面是一个基本的示例代码: ```csharp using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Windows.Forms; namespace JsonConfigEditor { public partial class MainForm : Form { private string _configFilePath = "config.json"; private List<ConfigItem> _configItems = new List<ConfigItem>(); public MainForm() { InitializeComponent(); } private void MainForm_Load(object sender, EventArgs e) { // 读取配置文件 if (File.Exists(_configFilePath)) { string json = File.ReadAllText(_configFilePath); _configItems = JsonConvert.DeserializeObject<List<ConfigItem>>(json); } // 绑定DataGridView控件 dataGridView1.DataSource = _configItems; } private void btnSave_Click(object sender, EventArgs e) { // 保存配置文件 string json = JsonConvert.SerializeObject(_configItems); File.WriteAllText(_configFilePath, json); } private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { // 更新配置项 if (e.RowIndex >= 0 && e.ColumnIndex >= 0) { var configItem = _configItems[e.RowIndex]; switch (dataGridView1.Columns[e.ColumnIndex].Name) { case "Key": configItem.Key = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(); break; case "Value": configItem.Value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(); break; } } } private void btnAdd_Click(object sender, EventArgs e) { // 添加新的配置项 _configItems.Add(new ConfigItem { Key = "", Value = "" }); dataGridView1.DataSource = null; dataGridView1.DataSource = _configItems; } private void btnDelete_Click(object sender, EventArgs e) { // 删除选中的配置项 foreach (DataGridViewRow row in dataGridView1.SelectedRows) { _configItems.RemoveAt(row.Index); } dataGridView1.DataSource = null; dataGridView1.DataSource = _configItems; } private void btnSearch_Click(object sender, EventArgs e) { // 查找关键字匹配的配置项 string keyword = txtKeyword.Text.Trim(); if (!string.IsNullOrEmpty(keyword)) { var result = _configItems.FindAll(item => item.Key.Contains(keyword) || item.Value.Contains(keyword)); dataGridView1.DataSource = null; dataGridView1.DataSource = result; } else { dataGridView1.DataSource = null; dataGridView1.DataSource = _configItems; } } } public class ConfigItem { public string Key { get; set; } public string Value { get; set; } } } ``` 在这个示例代码中,我们使用了一个List<ConfigItem>来存储读入的json数据,并将其绑定到了DataGridView控件上。然后,我们实现了一些按钮的点击事件,用于添加、删除、保存、查找配置项。在DataGridView控件的CellValueChanged事件中,我们可以捕获用户对配置项的修改,并将其同步到_configItems列表中。最后,在保存配置文件时,我们将_configItems序列化为json格式,并写入到磁盘文件中。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值