gpt4 book ai didi

c# - 使用根中的数字对象反序列化 JSON

转载 作者:行者123 更新时间:2023-11-30 15:16:00 26 4
gpt4 key购买 nike

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

我知道 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

最佳答案

如果我们在 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 中使用的名称。

关于c# - 使用根中的数字对象反序列化 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50871385/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com