c# - 如何更新 JSON 字段
问题描述
我正在尝试更新 JSON 响应中的特定字段,但是我能够解析 JSON,但我无法更新字段值。
通过 JSON 响应解析并尝试使用
Operands_parse.SelectToken("FieldValue").Replace("1234,5678");
但是当替换正在改变不应该发生的响应格式时
JObject Operands_parse = (JObject)Operands[0];
Operands_parse.SelectToken("FieldValue").Replace("1234,5678");
下面是 JSON 片段:
"Predicate": {
"Field": null,
"FieldValue": null,
"FilterOperator": "Equals",
"Operands": [
{
"Field": {
"Name": "Participant.IsPrimary",
"Alias": "Participant Is Primary",
"ValueInput": "UserName",
"FieldType": "Boolean",
"SortProxyName": "IsPrimary",
"PrismFieldName": "IsPrimaryFA",
"PrismDisplayFieldName": null,
"FieldValue": null,
"IsCustom": false,
"DependentField": null,
"Disabled": false,
"PlannedForDeprecation": false
},
"FieldValue": [
"true"
]
输出:
"FieldValue":
"1234,
5678",
预期的:
"FieldValue": [
"1234",
"5678"
],
解决方案
将特定字段转换为 JArray 后,事情就开始了,我可以从中更新值。
string[] CRs ;
CRs = new string[2] {"1234","5678"};
JObject Operands_parse = (JObject)Operands[0];
JToken FieldValue_t;
Operands_parse.TryGetValue("FieldValue", out FieldValue_t);
JArray item = (JArray)FieldValue_t;
/*Removed old values*/
item.RemoveAt(0);
item.RemoveAt(1);
/*Added New Values*/
item.Add(items_no[0]);
item.Add(items_no[1]);
`
"Predicate": { "Field": null, "FieldValue": null, "FilterOperator": "Equals", "Operands": [ { "Field": { "Name": "ChangeRequestParticipant.IsPrimary", "Alias": "Participant Is Primary", "ValueInput": "UserName", "FieldType": "Boolean", "SortProxyName": "ChangeRequestParticipant.IsPrimary", "PrismFieldName": "IsPrimaryFA", "PrismDisplayFieldName": null, "FieldValue": null, "IsCustom": false, "DependentField": null, "Disabled": false, "PlannedForDeprecation": false }, "FieldValue": [ "1234", "5678" ],`
推荐阅读
- wordpress - Divi 主题与 Yoast SEO 发生冲突
- javascript - 如果没有设置值,如何在下拉列表中选择选项(selenium webdriver-js)
- python - 如何使 s3 存储桶子图像迭代通用
- python - TypeError:查询描述符不是 numpy 数组,也不是标量
- sql - 选择第二行值出现,覆盖第一行值
- java - 如何将 Oracle 驱动程序添加到 Arquillian 的托管 Wildfly?
- php - 使用(不断更新)来自数据库的经纬度在谷歌地图上显示实时位置
- selenium - 无法在 selenium-cucumber 框架中实例化类
- reactjs - 反应路由。不能让它工作
- php - 获取两个字符之间的字符串MYSQL