C# 使用根目录中的数字对象反序列化JSON

C# 使用根目录中的数字对象反序列化JSON,c#,json.net,C#,Json.net,我收到以下JSON: { "1": { "startDate": "", "endDate": "", "projectId": 10000, "build": "", "totalExecutions": 1, "totalExecuted": 1, "environment": "", "description": "Audit Test Cycle", "executionSummaries": { "

我收到以下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}
}

其中一个根对象是recordcount,但其他对象是数字。这些数字很重要,所以我不能忘记。数字不是以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; }
}
公共类循环//:字典
{
公共字典循环{get;set;}
public int recordsCount{get;set;}
}
公课循环
{
公共整数{get;set;}
公共字符串endDate{get;set;}
公共字符串说明{get;set;}
公共整数{get;set;}
已启动公共字符串{get;set;}
公共字符串versionName{get;set;}
公共字符串扩展{get;set;}
公共字符串项目密钥{get;set;}
public int versionId{get;set;}
公共字符串环境{get;set;}
公共字符串生成{get;set;}
通过{get;set;}创建的公共字符串
公共字符串以{get;set;}结尾
公共字符串名称{get;set;}
公共字符串由{get;set;}修改
公共int projectId{get;set;}
公共字符串createdByDisplay{get;set;}
公共字符串startDate{get;set;}
}
我知道JSON不好,但我不能改变这一点。它来自第三方工具

通常我会这么做

return _jsonDeserializer.Deserialize<Cycles>(response);
return\u jsonDeserializer.反序列化(响应);
但这不起作用。 所以我试着不去“反序列化”它:

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;
Cycles result=新周期();
JObject-jsonOb=JObject.Parse(response.Content);
result.recordScont=jsonOb.Value(“recordScont”);
jsonOb.Remove(“recordsCount”);
var values=jsonOb.ToObject();
result.cycles=数值;
我可以获取“recordscount”数据,但在尝试创建字典时,我收到了错误

error CS0119: 'Dictionary<string, Cycle>' is a type, which is not valid in the given contex
错误CS0119:“Dictionary”是一种类型,在给定上下文中无效

如果JSON中没有
recordsCount
属性,那么它会相当简单。事实上,我认为最简单的方法是首先使用LINQ转换为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));
    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用Newtonsoft.Json.Linq;
课堂测试
{
静态void Main()
{
var json=File.ReadAllText(“test.json”);
var parsed=JObject.Parse(json);
var周期=新周期
{
cycles=parsed.Properties()
.Where(p=>int.TryParse(p.Name,out_u2;))
.ToDictionary(p=>p.Name,p=>p.Value.ToObject()),
RecordScont=(int)已解析[“RecordScont”]
};
WriteLine(string.Join(“,”,cycles.cycles.Keys));
}
}

另外,我建议将所有属性重命名为惯用的C#one,并使用
[JsonProperty]
指定JSON中使用的名称。

到目前为止您尝试了什么?在原始帖子中添加了我的尝试。您给出的确切代码对我有效-您从哪里得到错误?您可以将JSON更改为以下内容:
“recordcount”:“1”{object},“recordcount”:“2”{另一个object}
etc这会更容易处理。@DavidH:我很高兴它能工作——但正如我之前所说的,你问题中的代码对我也很有用。不过,至少你现在有两种方法可以做到这一点:)