将key:value对添加到JToken / JArray

时间:2019-04-22 16:19:09

标签: c# arrays json .net-core

我有一个JSON文档,其中包含一些数据,如下所示:

  ...
        backend:
          serviceName: test
          servicePort: 80

我想做的是在JToken / JObject中添加一个key:value对,其中确认号是一个特定的。对于这种情况,我想在测试数组内的确认编号为[ { "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 /对象中添加一个键值对("actions":"Launch, Ready, Done")。这是我到目前为止所做的

"1-3966461574"

这是我受困的地方,因为我不知道如何进一步进行。

1 个答案:

答案 0 :(得分:1)

您可以尝试这样:

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";
        }
    }

}