簡體   English   中英

將key:value對添加到JToken / JArray

[英]Adding a key:value pair to a 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中添加一個key:value對,其中確認號是一個特定的對。 對於這種情況,我想將一個鍵值對("actions":"Launch, Ready, Done")到{}中的Jtoken /對象,其中test數組中的確認號為"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";
        }
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM