是什么导致了 JsonException:无法转换 JSON 值?

What's causing JsonException: The JSON value could not be converted?

C# 10/.NET 6/System.Text.Json

我正在使用 API 作为 JSON 响应的 returns。我正在尝试使用 System.Text.Json 将 JSON 响应反序列化为 class。我收到了一个 JsonException,可以帮助我理解我做错了什么。

我调用 API 并存储 JSON 响应: string json = await Retreive.Fetch(target);

这是 Console.WriteLine(json) 的输出:

[{"id": 1148082,"name": "TestGroup","group_type":"console_group","provisioning_guid": null,"member_count": 1,"current_risk_score": 36.3,"status": "active"},{"id": 1148788,"name": "Group2","group_type": "smart_group","provisioning_guid": null,"member_count": 9,"current_risk_score": 39.7,"status": "active"},{"id": 1148792,"name": "Group3","group_type": "smart_group","provisioning_guid": null,"member_count": 9,"current_risk_score": 39.7,"status": "active"}]

如果有帮助,这里是一个印刷精美的版本:

[
  {
    "id": 1148082,
    "name": "TestGroup",
    "group_type": "console_group",
    "provisioning_guid": null,
    "member_count": 1,
    "current_risk_score": 36.3,
    "status": "active"
  },
  {
    "id": 1148788,
    "name": "Group2",
    "group_type": "smart_group",
    "provisioning_guid": null,
    "member_count": 9,
    "current_risk_score": 39.7,
    "status": "active"
  },
  {
    "id": 1148792,
    "name": "Group3",
    "group_type": "smart_group",
    "provisioning_guid": null,
    "member_count": 9,
    "current_risk_score": 39.7,
    "status": "active"
  }
]

使用Visual Studio 2022's Paste JSON作为类函数,我得到以下class结构:

public class Rootobject
{
    public Class1[] Property1 { get; set; }
}

public class Class1
{
    public int id { get; set; }
    public string name { get; set; }
    public string group_type { get; set; }
    public object provisioning_guid { get; set; }
    public int member_count { get; set; }
    public float current_risk_score { get; set; }
    public string status { get; set; }
}

我正在尝试:Rootobject? gag = JsonSerializer.Deserialize<Rootobject>(json);

抛出 JsonException:

Unhandled exception. System.Text.Json.JsonException: The JSON value could not be converted to KB4.Rootobject. Path: $ | LineNumber: 0 | BytePositionInLine: 1. at System.Text.Json.ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type propertyType) at System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value) at System.Text.Json.Serialization.JsonConverter1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value) at System.Text.Json.Serialization.JsonConverter1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state) at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan1 utf8Json, JsonTypeInfo jsonTypeInfo, Nullable1 actualByteCount) at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan1 json, JsonTypeInfo jsonTypeInfo) at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options) at KB4.Kb4.Main() in C:<REDACTED>\Program.cs:line 188 at KB4.Kb4.()

我尝试过的一些事情:

以上都不会产生不同的结果。

我做错了什么?

如果您的json以此

开头,您的根对象class将会工作
{ "property1": [{"id": 1148082,"name": .....] }

并且数组有一个 属性 名称。但是你的 json 没有 属性1,所以你应该开始直接反序列化 json 数组

Class1[] result = System.Text.Json.JsonSerializer.Deserialize<Class1[]>(json);