.net c# wpf Json读写以及修改保存 库文件为Newtonsoft.Json

48 篇文章 5 订阅
34 篇文章 32 订阅

使用的库文件为Newtonsoft.Json

读取JSON文件

 /// <summary>
        /// 读取JSON文件
        /// </summary>
        /// <param name="key">JSON文件中的key值</param>
        /// <returns>JSON文件中的value值</returns>
        public JObject Readjson(string jsonfile)
        {
            using (System.IO.StreamReader file = System.IO.File.OpenText(jsonfile))
            {
                using (JsonTextReader reader = new JsonTextReader(file))
                {
                    JObject jObject = (JObject)JToken.ReadFrom(reader);
                    return jObject;
                }
            }
        }

写JSON文件

   public void Writejson(string jsonfile, JObject jObject)
        {
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(jsonfile))
            {
                file.Write(jObject.ToString());
            }
        }

读JSON数组等,包括可能没有字段怎么处理

public ParameterItem ReadParameter()
        {
            try
            {
                JObject jObject = Readjson("a.json");
                JArray items = JArray.Parse(jObject["items"].ToString());
                foreach (var item in items)
                {
                    JObject itmeTemp = JObject.Parse(item.ToString());
                    if (uint.Parse(itmeTemp["channel"].ToString()) == channel)
                    {
                        ParameterItem parameterItem = new ParameterItem();
                        parameterItem.Channel = uint.Parse(itmeTemp["channel"].ToString());
                        parameterItem.Type = int.Parse(itmeTemp["type"].ToString());
                        parameterItem.Name = itmeTemp["name"].ToString();
                        if (itmeTemp.Property("maintenance") != null)
                        {
                            parameterItem.Maintenance = bool.Parse(itmeTemp["maintenance"].ToString()); ;
                        }
                        else
                        {
                            parameterItem.Maintenance = true;
                        }
                        return ParameterItem;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("读取配置参数出现异常!"+ ex);
                return null;
            }
            return null;
        }

修改后写入

JObject jObject = Readjson("a.json");
                JToken items = jObject["items"];
                foreach (var item in items)
                {
                    for (int i = 0; i < newItem.Count; i++)
                    {
                        if (uint.Parse(item["channel"].ToString()) == newItem[i].Channel)
                        {
                            item["type"]  = newItem[i].Type;
                            item["oneToTwenty"] = newItem[i].OneToTwenty;
                            break;
                        }
                    }
                }
                Writejson("a.json", jObject);

JSON文件结构

{
	"count":2,
	"items": [
	{
		"channel": 0,
		"type":0,
		"name":"测试0通道",
	},
	{
		"channel": 1,
		"type":1,
		"name":"测试1通道",
	}]
}

举一反三可以实现很多功能

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
In .NET 6 and later versions, the `System.Windows.Forms` namespace is not included in the default installation of WPF applications. However, you can still use the `FolderBrowserDialog` class in your WPF application by adding a reference to the `System.Windows.Forms` NuGet package and using the `System.Windows.Forms.Integration` namespace. Here are the steps to use `FolderBrowserDialog` in .NET 6 WPF: 1. Add a reference to the `System.Windows.Forms` NuGet package in your WPF project. 2. Add the following using statements to the top of your code file: ```csharp using System.Windows.Forms; using System.Windows.Forms.Integration; ``` 3. In your code, create a `FolderBrowserDialog` object and use the `ShowDialog` method to display the dialog box: ```csharp FolderBrowserDialog dialog = new FolderBrowserDialog(); if (dialog.ShowDialog() == DialogResult.OK) { // do something with the selected folder } ``` 4. If you want to use the `FolderBrowserDialog` in a WPF window or page, you can use the `WindowsFormsHost` control to host the dialog box: ```xml <Window xmlns:winforms="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration" xmlns:forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" ...> <Grid> <winforms:WindowsFormsHost> <forms:FolderBrowserDialog x:Name="folderDialog" /> </winforms:WindowsFormsHost> </Grid> </Window> ``` Then, you can use the `ShowDialog` method to display the dialog box: ```csharp if (folderDialog.ShowDialog() == DialogResult.OK) { // do something with the selected folder } ``` Note that you will also need to add a reference to the `WindowsFormsIntegration` assembly in your WPF project.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花开花落的个人博客

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值