首页 > 解决方案 > 使用根目录中的数字对象反序列化 JSON

问题描述

我收到以下 JSON:

{
"1": {
    "startDate": "",
    "endDate": "",
    "projectId": 10000,
    "build": "",
    "totalExecutions": 1,
    "totalExecuted": 1,
    "environment": "",
    "description": "Audit Test Cycle",
    "executionSummaries": {
        "executionSummary": [
            {
                "count": 0,
                "statusKey": -1,
                "statusName": "UNEXECUTED",
                "statusColor": "#A0A0A0",
                "statusDescription": "The test has not yet been executed."
            },
            {
                "count": 1,
                "statusKey": 1,
                "statusName": "PASS",
                "statusColor": "#75B000",
                "statusDescription": "Test was executed and passed successfully."
            },
            {
                "count": 0,
                "statusKey": 2,
                "statusName": "FAIL",
                "statusColor": "#CC3300",
                "statusDescription": "Test was executed and failed."
            },
            {
                "count": 0,
                "statusKey": 3,
                "statusName": "WIP",
                "statusColor": "#F2B000",
                "statusDescription": "Test execution is a work-in-progress."
            },
            {
                "count": 0,
                "statusKey": 4,
                "statusName": "BLOCKED",
                "statusColor": "#6693B0",
                "statusDescription": "The test execution of this test was blocked for some reason."
            }
        ]
    },
    "name": "Audit Test Cycle",
    "expand": "executionSummaries",
    "versionId": 10000,
    "started": ""
},
"2":  {
    "startDate": "",
    "endDate": "",
    "projectId": 10000,
    "build": "",
    "totalExecutions": 1,
    "totalExecuted": 1,
    "environment": "",
    "description": "Audit Test Cycle",
    "executionSummaries": {
        "executionSummary": [
            {
                "count": 0,
                "statusKey": -1,
                "statusName": "UNEXECUTED",
                "statusColor": "#A0A0A0",
                "statusDescription": "The test has not yet been executed."
            },
            {
                "count": 1,
                "statusKey": 1,
                "statusName": "PASS",
                "statusColor": "#75B000",
                "statusDescription": "Test was executed and passed successfully."
            },
            {
                "count": 0,
                "statusKey": 2,
                "statusName": "FAIL",
                "statusColor": "#CC3300",
                "statusDescription": "Test was executed and failed."
            },
            {
                "count": 0,
                "statusKey": 3,
                "statusName": "WIP",
                "statusColor": "#F2B000",
                "statusDescription": "Test execution is a work-in-progress."
            },
            {
                "count": 0,
                "statusKey": 4,
                "statusName": "BLOCKED",
                "statusColor": "#6693B0",
                "statusDescription": "The test execution of this test was blocked for some reason."
            }
        ]
    },
    "name": "Audit Test Cycle",
    "expand": "executionSummaries",
    "versionId": 10000,
    "started": ""
},
"3":  {
    "startDate": "",
    "endDate": "",
    "projectId": 10000,
    "build": "",
    "totalExecutions": 1,
    "totalExecuted": 1,
    "environment": "",
    "description": "Audit Test Cycle",
    "executionSummaries": {
        "executionSummary": [
            {
                "count": 0,
                "statusKey": -1,
                "statusName": "UNEXECUTED",
                "statusColor": "#A0A0A0",
                "statusDescription": "The test has not yet been executed."
            },
            {
                "count": 1,
                "statusKey": 1,
                "statusName": "PASS",
                "statusColor": "#75B000",
                "statusDescription": "Test was executed and passed successfully."
            },
            {
                "count": 0,
                "statusKey": 2,
                "statusName": "FAIL",
                "statusColor": "#CC3300",
                "statusDescription": "Test was executed and failed."
            },
            {
                "count": 0,
                "statusKey": 3,
                "statusName": "WIP",
                "statusColor": "#F2B000",
                "statusDescription": "Test execution is a work-in-progress."
            },
            {
                "count": 0,
                "statusKey": 4,
                "statusName": "BLOCKED",
                "statusColor": "#6693B0",
                "statusDescription": "The test execution of this test was blocked for some reason."
            }
        ]
    },
    "name": "Audit Test Cycle",
    "expand": "executionSummaries",
    "versionId": 10000,
    "started": ""
},
"recordsCount": 3}

}

根对象之一是记录计数,但其他的是数字。这些数字很重要,所以我不能丢失它们。该数字不以 1 递增,它可以是 8、5、2、4、6 或任何其他组合(但唯一!)。

我想将它们反序列化为以下类,但不知道如何处理该数字。

public class Cycles //: Dictionary<string,Cycle>
{
    public Dictionary<string,Cycle> cycles { get; set; }
    public int recordsCount { get; set; } 
}


public class Cycle 
{
    public int totalExecutions { get; set; }
    public string endDate { get; set; }
    public string description { get; set; }
    public int totalExecuted { get; set; }
    public string started { get; set; }
    public string versionName { get; set; }
    public string expand { get; set; }
    public string projectKey { get; set; }
    public int versionId { get; set; }
    public string environment { get; set; }
    public string build { get; set; }
    public string createdBy { get; set; }
    public string ended { get; set; }
    public string name { get; set; }
    public string modifiedBy { get; set; }
    public int projectId { get; set; }
    public string createdByDisplay { get; set; }
    public string startDate { get; set; }
}

我知道 JSON 不好,但我无法改变它。它来自第三方工具。

通常我会做

return _jsonDeserializer.Deserialize<Cycles>(response);

但这不起作用。所以我尽量不要“反序列化”它:

Cycles result = new Cycles();
        JObject jsonOb = JObject.Parse(response.Content);
        result.recordsCount = jsonOb.Value<int>("recordsCount");
        jsonOb.Remove("recordsCount");
        var values = jsonOb.ToObject<Dictionary<string,Cycle>>();
        result.cycles = values;

我可以获得“recordscount”数据,但是在尝试创建字典时收到错误

error CS0119: 'Dictionary<string, Cycle>' is a type, which is not valid in the given contex

标签: c#json.net

解决方案


如果我们在 JSON 中没有该recordsCount属性,那将相当简单。事实上,我怀疑最简单的方法是首先使用 LINQ to JSON,找出哪些值属于字典,然后要求 JSON.NET 转换其中的每一个……但自己填充顶级对象。(从外观上看,这很像您已经尝试过的。)

这是一个适用于您的示例数据和类的示例:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json.Linq;

class Test
{
    static void Main()
    {
        var json = File.ReadAllText("test.json");
        var parsed = JObject.Parse(json);
        var cycles = new Cycles
        {
            cycles = parsed.Properties()
                .Where(p => int.TryParse(p.Name, out _))
                .ToDictionary(p => p.Name, p => p.Value.ToObject<Cycle>()),                  
            recordsCount = (int) parsed["recordsCount"]
        };
        Console.WriteLine(string.Join(", ", cycles.cycles.Keys));
    }
}

顺便说一句,我建议将所有属性重命名为惯用的 C# 属性,并用于[JsonProperty]指定 JSON 中使用的名称。


推荐阅读