使用C#保存在现有JSON文件中 [英] Saving in existing JSON file using C#

查看:509
本文介绍了使用C#保存在现有JSON文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将数据保存在JSON文件中。必须在现有类别下插入数据。我无法在所需的类别下编写数据,而是将其作为单独的类别。请找到下面的代码。

CategoryName:[{
SubCategoryName:Test2,
HostName:111.222 .225.15,
UserName:AAAA,
密码:AAAA,
IsActive:true
},
{
SubCategoryName:test 1,
HostName:11.11.11.11,
UserName:3333,
密码:33333,
IsActive:false
}]



我想添加第三个子类别,但我无法做到这一点?



我尝试过:



我试图从现有数据中读取数据json文件并将其分配给对象并尝试再次写入该文件。

我无法将新数据写入文件。

解决方案

转换为类,添加额外数据,然后转换回来并保存JSON。本文将向您展示如何:在C#中使用JSON& VB [ ^ ]

I have to save data in a JSON file. the data has to be inserted under a existing category. I am not able to write the data under the required category, instead it is coming as a seperate one. Please find the code below.

"CategoryName": [{
	"SubCategoryName": "Test2",
	"HostName": "111.222.225.15",
	"UserName": "AAAA",
	"Password": "AAAA",
	"IsActive": true
},
{
	"SubCategoryName": "test 1",
	"HostName": "11.11.11.11",
	"UserName": "3333",
	"Password": "33333",
	"IsActive": false
}]


I want to add a third Sub Category, but i am unable to do it?

What I have tried:

I have tried to read the data from the existing json file and assign it to a object and tried to write into the file again.
I am not able to write the new data into the file.

解决方案

Convert to classes, Add the extra data, then convert back and save the JSON. This article will show you how: Working with JSON in C# & VB[^]


这篇关于使用C#保存在现有JSON文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆