解析包含未命名节点的JSON

时间:2018-05-24 09:03:54

标签: c# json parsing json.net

我有以下JSON有效负载。

挑战是获取名称值和相应的根节点号(3925或3932),因为此节点未命名。

我正在使用C#

"3925": {
        "totalExecutions": 2,
        "endDate": "",
        "description": "",
        "totalExecuted": 2,
        "started": "",
        "versionName": "Custom Pipes Development",
        "expand": "executionSummaries",
        "projectKey": "WUSDV007",
        "versionId": 22361,
        "environment": "",
        "totalCycleExecutions": 2,
        "totalDefects": 0,
        "build": "",
        "createdBy": "mghosh",
        "ended": "",
        "name": "SetMaxFutureDateFromCustomerField_Mobile",
        "totalFolders": 0,
        "modifiedBy": "mghosh",
        "projectId": 17101,
        "createdByDisplay": "Mayukh Ghosh",
        "startDate": "",
        }
    }

"3932": {
        "totalExecutions": 2,
        "endDate": "",
        "description": "",
        "totalExecuted": 2,
        "started": "",
        "versionName": "Custom Pipes Development",
        "expand": "executionSummaries",
        "projectKey": "WUSDV007",
        "versionId": 22361,
        "environment": "",
        "totalCycleExecutions": 2,
        "totalDefects": 0,
        "build": "",
        "createdBy": "nkonda",
        "ended": "",
        "name": "WUSDV007-29779-Fee Validation",
        "totalFolders": 0,
        "modifiedBy": "nkonda",
        "projectId": 17101,
        "createdByDisplay": "Naveen Kumar Konda",
        "startDate": "",

    }

非常感谢任何建议。

1 个答案:

答案 0 :(得分:0)

尝试以下代码:

string jsonString = "{\"3925\": { \"totalExecutions\": 2, \"endDate\": \"\", \"description\": \"\", \"totalExecuted\": 2, \"started\": \"\", \"versionName\": \"Custom Pipes Development\", \"expand\": \"executionSummaries\", \"projectKey\": \"WUSDV007\", \"versionId\": 22361, \"environment\": \"\", \"totalCycleExecutions\": 2, \"totalDefects\": 0, \"build\": \"\", \"createdBy\": \"mghosh\", \"ended\": \"\", \"name\": \"SetMaxFutureDateFromCustomerField_Mobile\", \"totalFolders\": 0, \"modifiedBy\": \"mghosh\", \"projectId\": 17101, \"createdByDisplay\": \"Mayukh Ghosh\", \"startDate\": \"\" }}";

var parameters = JObject.Parse(jsonString);

foreach (var item in parameters.OfType<JProperty>())
{
   var innerObject = JObject.Parse(item.Value.ToString());
   Console.WriteLine(innerObject.ToString());
}