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

iugsix8n  于 2024-01-09  发布在  其他
关注(0)|答案(2)|浏览(125)

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"}]

字符串
这里有一个漂亮的印刷版本,如果它有帮助:

[
  {
    "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的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; }
}


我正在尝试:Rootobject? gag = JsonSerializer.Deserialize<Rootobject>(json);
抛出JsonException:
System.Text.Json.JsonException:无法将JSON值转换为KB4.RootObject。路径:$|LineNumber:0| BytePositionInLine:1.在System.Text.Json.ThrowHelper.ThrowJsonException_UnializeUnableToConvertValue(Type propertyType)位于System.Text.Json.Serialization.Converters.ObjectDefaultConverter 1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value) at System.Text.Json.Serialization.JsonConverter 1.TryRead(Utf8JsonReader& reader,Type ToConvert,JsonSerializerOptions options,ReadStack& state,T& value)at System.Text.Json.Serialization.JsonConverter 1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state) at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan 1 utf8Json,JsonTypeInfo jsonTypeInfo,Nullable 1 actualByteCount) at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan 1 json,JsonTypeInfo(JsonTypeInfo)位于System.Text.Json.JsonSerializer. Jsonialize [TValue](String json,JsonSerializerOptions选项)位于KB4.Kb4.Main()在C:\Program.cs中:第188行位于KB4.Kb4.()
我尝试过的一些事情:

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

上述两种方法都不会产生不同的结果。
我做错了什么?

8gsdolmq

8gsdolmq1#

如果你的json以这个开头,

{ "property1": [{"id": 1148082,"name": .....] }

字符串
数组有一个属性名,但你的json没有property1,所以你应该直接初始化json数组

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

vjhs03f7

vjhs03f72#

这也应该起作用:

var response = JsonSerializer.Deserialize<List<Class1>>(json);

字符串

相关问题