c# - C# 将值添加到 JSON 文件而不更改初始 JSON 格式
问题描述
我想要的是:
在 JSON 文件中的特定位置添加属性(对于这种情况,我想在 {"name":"Andrew"} 之后添加 {"name2":"Tom} )而不更改其格式(空的新行)
当前 JSON
{
"Profiles": [
{
"Properties": {
"color": "blue",
"shape": "circle",
"fruit": "apple",
"name": "Andrew",
"username": "ad11",
"Password": "pass",
"country": "France",
"region": "Europe"
}
}
]
}
当前代码
var json = File.ReadAllText(path);
var Jobect = JObject.Parse(json);
Jobect["Profiles"][0]["Properties"]["name2"] = "Tom";
File.WriteAllText(path, Jobect.ToString());
结果 JSON
{
"Profiles": [
{
"Properties": {
"color": "blue",
"shape": "circle",
"fruit": "apple",
"name": "Andrew",
"username": "ad11",
"Password": "pass",
"country": "France",
"region": "Europe",
"name2": "Tom"
}
}
]
}
所需的 JSON
{
"Profiles": [
{
"Properties": {
"color": "blue",
"shape": "circle",
"fruit": "apple",
"name": "Andrew",
"name2": "Tom,
"username": "ad11",
"Password": "pass",
"country": "France",
"region": "Europe"
}
}
]
}
综上所述。如何在 JSON 文件中的特定位置添加属性而不更改其格式?
解决方案
推荐阅读
- amazon-ec2 - 使用 Systemd 在 EC2 Centos 上运行 Airflow 网络服务器:权限被拒绝
- javascript - Google Scripts Web App 未使用数据填充 Googlesheet
- python - pip install pdfminer 错误(Windows 上的 python 2)
- f# - 返回任务
在 F# 中,语法是什么? - django - 在 Django Dojo 中使用 bootswatch
- assembly - 如何用 AND 重写(P 或 Q)
- python - 数据框熊猫在列中找到正确的值
- wordpress - 标题显示动态图标后的伪元素
- git - 为许多开发人员在 git repo 中设置文件权限
- c# - 如何在 C# 中使用存储过程结果作为子查询