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

chr*_*ire 14 .net c# json system.text.json

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

我正在使用一个以 JSON 响应形式返回的 API。我正在尝试System.Text.Json将 JSON 响应反序列化为一个类。我收到 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"}]
Run Code Online (Sandbox Code Playgroud)

如果有帮助的话,这是一个漂亮的印刷版本:

[
  {
    "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"
  }
]
Run Code Online (Sandbox Code Playgroud)

使用 Visual Studio 2022 的 Paste JSON As Classes 功能,我得到以下类结构:

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; }
}
Run Code Online (Sandbox Code Playgroud)

我想:Rootobject? gag = JsonSerializer.Deserialize<Rootobject>(json);

抛出 JsonException:

未处理的异常。System.Text.Json.JsonException:无法将 JSON 值转换为 KB4.Rootobject。路径: $ | 行号: 0 | BytePositionInLine:1.在System.Text.Json.ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(类型propertyType)在System.Text.Json.Serialization.Converters.ObjectDefaultConverter 1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value) at System.Text.Json.Serialization.JsonConverter1.TryRead(Utf8JsonReader&读者,类型typeToConvert,JsonSerializerOptions选项,ReadStack&状态,T&值)在System.Text.Json.Serialization.JsonConverter 1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state) at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan1 utf8Json,JsonTypeInfo jsonTypeInfo,Nullable 1 actualByteCount) at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan1 json,JsonTypeInfo jsonTypeInfo)在System.Text.Json.JsonSerializer.Deserialize [TValue](字符串json,JsonSerializerOptions选项)在KB4.Kb4.Main()在 C:<已编辑>\Program.cs:KB4.Kb4.() 第 188 行

我尝试过的一些事情:

  • 将类的名称更改RootobjectGetAllGroups
  • 考虑到响应中的 JSON 可能存在某种格式错误,我将其粘贴到一个文本文件中并从那里加载 JSON,然后再次尝试反序列化。
  • 查看了在 C# 中反序列化 JSON 数组,但那是使用 JavaScriptSerializer。

上述两者都不会产生不同的结果。

我究竟做错了什么?

Ser*_*rge 14

如果你的 json 以此开头,你的 Rootobject 类就会工作

{ "property1": [{"id": 1148082,"name": .....] }
Run Code Online (Sandbox Code Playgroud)

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

Class1[] result = System.Text.Json.JsonSerializer.Deserialize<Class1[]>(json); 
Run Code Online (Sandbox Code Playgroud)