c# - 在 C# 中更新嵌套数组中键值对的值
问题描述
我有以下 json
{"audit_entry": {
"where_uri": "test.com/service/apps/171f0841-825b-4964-8f8c-0869650f14a6",
"why_uri": "test.com/service/reference/reasons_for_change/43545kjhjhkj0869650f14a6",
"who_uri": "test.com/service/users/4977dae1-a307-425f-980c-53413fef1b0f",
"when_audited": "2018-11-13T20:20:39+00:00",
"what_uri": "test.com/service/subjects/1bc67a71-8549-4ab8-9dd9-e44238198860",
"what_changed": [
{
"attribute_name": "birth_year",
"attribute_value": "1969",
"attribute_change": null
},
{
"attribute_name": "subject_reference",
"attribute_value": "dsdsfg",
"attribute_change": null
}
]
}
我希望能够在 what_changed 的第二个孩子中更改“attribute_value”的值。即索引[1]。我尝试了以下代码:
JObject jObj = JObject.Parse(jsonText);
jObj["audit_entry"]["what_changed"]["attribute_name"[1]];
但我知道我的语法有问题。任何想法将不胜感激。谢谢
解决方案
这是有关使用属性名称或集合索引获取值JObject
的文档。
您可以在代码示例中看到索引的用法:
string itemTitle = (string)rss["channel"]["item"][0]["title"];
在您的代码示例中,它应该是:
var toto = (string)jObj["audit_entry"]["what_changed"][1]["attribute_name"];
您可以使用修改 JSON作为修改的参考:
jObj["audit_entry"]["what_changed"][1]["attribute_name"] = "New Value";
推荐阅读
- python - 将三个矩阵重整为一个
- php - 在 WooCommerce 中获取购物车项目变体名称而不是变体 ID
- airflow - 在 Composer 中将 airflow_local_settings.py 放在哪里?
- python - Nextflow:python脚本不保存输出
- powershell - Powershell - 如何搜索具有已知子项名称和值的散列注册表项
- mysql - 如何使用jpa将数据保存在链接表中
- reactjs - 防止反应路由器 ping Django 服务器
- google-drive-api - 是否可以使用 Postman/HTTP 请求在 Google Drive 中创建文件夹?
- r - 将函数的输出向量应用于原始数据帧
- flutter - 在颤动中单击按钮时添加小部件时处理动态复选框列表