c# - 将键:值对添加到 JToken/JArray
问题描述
我有一个 JSON 文档,其中包含一些数据,如下所示:
[
{
"id": 1,
"candidate": {
"firstName": "Subahar",
"lastName": "Kumar",
"alias": "S K"
},
"seatNo": "WKS14",
"checkInStatus": "NoShow",
"tests": [
{
"examCode": "OI4-759F",
"examName": "OI Professional of Fish",
"confirmationNo": "1-3966461574",
"keyCode": "3bdb987e-3623-4edc-9c24-ec9652ac6ac8",
"startDate": "1/4/2019",
"scheduledDuration": "1",
"startTime": "1200",
"endTime": "1300",
"resultStatus": "NotApplicable",
"testStatus": "Voided"
},
{
"examCode": "CY6-628F",
"examName": "CY Quiz of Art",
"confirmationNo": "1-6221969273",
"keyCode": "2e08c13a-2e52-4bc6-a771-af1670d00d15",
"startDate": "1/4/2019",
"scheduledDuration": "1",
"startTime": "1200",
"endTime": "1300",
"resultStatus": "NotApplicable",
"testStatus": "Voided"
},
{
"examCode": "SH4-390F",
"examName": "SH Quiz of Physics",
"confirmationNo": "3-5058796552",
"keyCode": "4566b64f-80a3-409e-a0ab-736c8dcf07b6",
"startDate": "1/4/2019",
"scheduledDuration": "1",
"startTime": "1200",
"endTime": "1300",
"resultStatus": "NotApplicable",
"testStatus": "Voided"
}
],
"candidateFirstName": "Subahar",
"candidateLastName": "Kumar",
"candidateAlias": "S K"
},
{
"examCode": "CY7-356F",
"examName": "CY Verified of Art",
"confirmationNo": "8-8365446002",
"keyCode": "892b8218-f31a-4c69-bffe-6ff2d79999ee",
"startDate": "1/4/2019",
"scheduledDuration": "1",
"startTime": "1200",
"endTime": "1300",
"resultStatus": "NotUploaded",
"testStatus": "NotStarted",
"id": 2,
"candidate": {
"firstName": "Divya",
"lastName": "Swaminathan",
"alias": "D S"
},
"seatNo": "WKS13",
"checkInStatus": "CheckedIn",
"tests": [
{
"examCode": "CY7-356F",
"examName": "CY Verified of Art",
"confirmationNo": "8-8365446002",
"keyCode": "892b8218-f31a-4c69-bffe-6ff2d79999ee",
"startDate": "1/4/2019",
"scheduledDuration": "1",
"startTime": "1200",
"endTime": "1300",
"resultStatus": "NotUploaded",
"testStatus": "NotStarted"
}
],
"candidateFirstName": "Divya",
"candidateLastName": "Swaminathan",
"candidateAlias": "D S"
},
]
我想要做的是在 JToken/JObject 中添加一个键:值对,其中确认号是特定的。对于这种情况,我想将一个键值对添加("actions":"Launch, Ready, Done")
到 {} 内的 Jtoken/对象,其中确认号"1-3966461574"
位于测试数组中。这是我到目前为止所做的
JArray rosterData = GetRosterData();
foreach (var roster in rosterData.Children<JObject>()) {
foreach (var property in roster.Properties()) {
if (property.Name.Equals("tests")) {
}
}
}
这是我卡住的地方,因为我不知道如何进一步进行。
解决方案
你可以这样尝试:
static void Main(string[] args)
{
string input = @"
[
{
'id': 1,
'candidate': {
'firstName': 'Subahar',
'lastName': 'Kumar',
'alias': 'S K'
},
'seatNo': 'WKS14',
'checkInStatus': 'NoShow',
'tests': [
{
'examCode': 'OI4-759F',
'examName': 'OI Professional of Fish',
'confirmationNo': '1-3966461574',
'keyCode': '3bdb987e-3623-4edc-9c24-ec9652ac6ac8',
'startDate': '1/4/2019',
'scheduledDuration': '1',
'startTime': '1200',
'endTime': '1300',
'resultStatus': 'NotApplicable',
'testStatus': 'Voided'
},
{
'examCode': 'CY6-628F',
'examName': 'CY Quiz of Art',
'confirmationNo': '1-6221969273',
'keyCode': '2e08c13a-2e52-4bc6-a771-af1670d00d15',
'startDate': '1/4/2019',
'scheduledDuration': '1',
'startTime': '1200',
'endTime': '1300',
'resultStatus': 'NotApplicable',
'testStatus': 'Voided'
},
{
'examCode': 'SH4-390F',
'examName': 'SH Quiz of Physics',
'confirmationNo': '3-5058796552',
'keyCode': '4566b64f-80a3-409e-a0ab-736c8dcf07b6',
'startDate': '1/4/2019',
'scheduledDuration': '1',
'startTime': '1200',
'endTime': '1300',
'resultStatus': 'NotApplicable',
'testStatus': 'Voided'
}
],
'candidateFirstName': 'Subahar',
'candidateLastName': 'Kumar',
'candidateAlias': 'S K'
}
]";
JArray j = JArray.Parse(input);
foreach (JToken item in j)
{
foreach (JToken innerItem in item["tests"].Where(x => x["confirmationNo"].ToString() == "1-3966461574"))
{
innerItem["actions"] = "Launch, Ready, Done";
}
}
}
推荐阅读
- python - 使用正则表达式的错误 - 预期的字符串或类似字节的对象
- flutter - Flutter 如何在网格上的 TextFields 之间切换焦点(填字游戏)
- r - 在R中命名矩阵中的行
- javascript - 如何让访问我的站点的客户端发出另一个请求并在初始请求中使用其响应数据?
- python - Scrapy request get some responses, but not all
- php - 将 PHP 变量设置为 JSON 对象中的值
- wix - Wix:是否可以从控制面板的“添加/删除程序”中删除捆绑条目?
- docker - appId: error.pattern 在 Marathon 中创建应用程序时
- python - 在 mysql 函数中返回一个布尔值并在 python 中使用它
- java - Getter 和 Setter 的问题 [Java]