Was verursacht JSonexception: Der JSON -Wert konnte nicht konvertiert werden?C#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Was verursacht JSonexception: Der JSON -Wert konnte nicht konvertiert werden?

Post by Anonymous »

C# 10 / .net 6 / system.text.json < / p>
Ich arbeite mit einer API, die als JSON -Antwort zurückgibt. Ich versuche, System.text.json zu verwenden, um die JSON -Antwort in eine Klasse zu deserialisieren. Ich erhalte eine jSonexception und könnte Hilfe verwenden, um zu verstehen, was ich falsch mache.

Code: Select all

string json = await Retreive.Fetch(target);

Hier ist die Ausgabe von Console.WriteLine (JSON) :

Code: Select all

[{"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"}]
< /code>
Hier ist eine ziemlich gedruckte Version, wenn sie hilft: < /p>
[
{
"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"
}
]
< /code>
Verwenden von Visual Studio 2022 Paste JSON als Klassenfunktion, ich erhalte die folgende Klassenstruktur: < /p>
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; }
}
< /code>
Ich versuche: rootObject? gag = jsonSerializer.Deserialize  (JSON); 

a JSonexception wird geworfen:

Code: Select all

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.ObjectDefaultConverter`1.OnTryRead(Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options, ReadStack state, T value)    at
System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options, ReadStackstate, 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)    at
System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)    at KB4.Kb4.Main() in
C:\\Program.cs:line 188    at
KB4.Kb4.()
Einige Dinge, die ich ausprobiert habe:

Ändern des Namens des RootObject -Klasses, um Getallgroups zu erhalten. /> Bewertet Deserialize ein JSON -Array in C#, aber das verwendet JavaScriptSerializer.>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post