簡體   English   中英

System.Text.Json.JsonException:JSON 值無法轉換為 System.Collections.Generic.List`1[System.Collections.Generic.List`1

[英]System.Text.Json.JsonException: The JSON value could not be converted to System.Collections.Generic.List`1[System.Collections.Generic.List`1

我是 C# 開發的初學者,我需要你的幫助。我正在檢查 Stackoverflow 中的所有主題,但找不到並解決我的問題。 我收到一個 API 的 JSON 響應,我想轉換 object 中的數據。

----------這里是我的 JSON 回復:

 {"code":200,"status":"success","message":"Request completed","data":      \[\[{"Matricule_Collaborateur":"455","Nom_Prenom_Collaborateur":"lastname_455 firstname_455","Jour_Presence":"01-12-2022","Adresse_Postale_Collaborateur":"","Code_Postal_Collaborateur":"","Ville_Collaborateur":"","Date_Naissance_Collaborateur":"01-01-1980","Titre_Collaborateur":null,"Email_Pro_Collaborateur":"email_455","Identifiant_Collaborateur":"CERFRANCE Finist\\u00e8re"}\],.......

----------如果我用 JObject.Parse 解析它:

{
"Matricule_Collaborateur": "455",
"Nom_Prenom_Collaborateur": "lastname_455 firstname_455",
"Jour_Presence": "01-12-2022",
"Adresse_Postale_Collaborateur": "",
"Code_Postal_Collaborateur": "",
"Ville_Collaborateur": "",
"Date_Naissance_Collaborateur": "01-01-1980",
"Titre_Collaborateur": null,
"Email_Pro_Collaborateur": "email_455",
"Identifiant_Collaborateur": "CERFRANCE Finistère"
}
\],
\[
{
"Matricule_Collaborateur": "455",
"Nom_Prenom_Collaborateur": "lastname_455 firstname_455",
"Jour_Presence": "02-12-2022",
"Adresse_Postale_Collaborateur": "",
"Code_Postal_Collaborateur": "",
"Ville_Collaborateur": "",
"Date_Naissance_Collaborateur": "01-01-1980",
"Titre_Collaborateur": null,
"Email_Pro_Collaborateur": "email_455",
"Identifiant_Collaborateur": "CERFRANCE Finistère"
}
\],
\[
{
"Matricule_Collaborateur": "455",
"Nom_Prenom_Collaborateur": "lastname_455 firstname_455",
"Jour_Presence": "05-12-2022",
"Adresse_Postale_Collaborateur": "",
"Code_Postal_Collaborateur": "",
"Ville_Collaborateur": "",

……

----------這里是我的 Class:

public class PresenceDay
{
    //Properties
    [JsonPropertyName("Matricule_Collaborateur")]
     public string? Matricule { get; set; }

    [JsonPropertyName("Nom_Prenom_Collaborateur")]
    public string? Nom_Prenom { get; set; }

    [JsonPropertyName("Jour_Presence")]
    public string? Jour_Presence { get; set; }

    [JsonPropertyName("Adresse_Postale_Collaborateur")]
    public string? Adresse_Postale { get; set; }

    [JsonPropertyName("Code_Postal_Collaborateur")]
    public string? Code_Postal { get; set; }

    [JsonPropertyName("Ville_Collaborateur")]
    public string? Ville { get; set; }

    [JsonPropertyName("Date_Naissance_Collaborateur")]
    public string? Date_Naissance { get; set; }

    [JsonPropertyName("Titre_Collaborateur")]
    public string? Titre_Poste { get; set; }

    [JsonPropertyName("Email_Pro_Collaborateur")]
    public string? Email_Pro { get; set; }

    [JsonPropertyName("Identifiant_Collaborateur")]
    public string? Identifiant_Collaborateur { get; set; }
}

----------這里是我的 Program.cs:

try
{
Console.WriteLine("--------------------");
Console.WriteLine("Resultat API Jours de Présence :");
Console.WriteLine("--------------------");

            var options = new JsonSerializerOptions
            {
                PropertyNameCaseInsensitive = true,
                NumberHandling = JsonNumberHandling.AllowReadingFromString
            };
            List<PresenceDay> obj_day = JsonSerializer.Deserialize<List<PresenceDay>>(result_day, options);
            //Console.WriteLine(obj_day);

            foreach (var day in obj_day)
            {
                Console.WriteLine(day);
                Console.WriteLine(day.Matricule);

            }

        }

(No compilation errors here)

catch(Exception ex)
{
Console.WriteLine("--------------------");
Console.WriteLine($"{ex}");
Console.WriteLine("--------------------");
Console.WriteLine("Resultat : ERREUR ");
Console.WriteLine("--------------------");
}

----------我明白了:

--------------------
Resultat API Jours de Présence :
--------------------

System.Text.Json.JsonException: The JSON value could not be converted to System.Collections.Generic.List`1[Cf29.TicketRestos.Entities.PresenceDay]. Path: $ | LineNumber: 0 | BytePositionInLine: 1.    at System.Text.Json.ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type propertyType)    at System.Text.Json.Serialization.JsonCollectionConverter`2.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, TCollection& value)
at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, 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)
at System.Text.Json.JsonSerializer.Deserialize\[TValue\](String json, JsonSerializerOptions options)
at Cf29.TicketRestos.Program.Main(String\[\] args) in C:\\Users\\1643\\source\\repos\\Cf29.TicketRestos\\Cf29.TicketRestos\\Program.cs:line 84
--------------------
Resultat : ERREUR
--------------------

希望你能幫我

你的 JSON 壞了。 它不是 JSON。它應該是[而不是\[並且同樣是右括號。

如果您將其輸入損壞的 JSON,解析器將無能為力。

您需要修復數據。

你的 json 無效,我先修好了

json = json.Replace("\\[\\[","[").Replace("\\]","]");
var jsonObject = JsonObject.Parse(json);

var options = new JsonSerializerOptions
{
        PropertyNameCaseInsensitive = true,
        NumberHandling = JsonNumberHandling.AllowReadingFromString
};
List<PresenceDay> obj_day = jsonObject["data"].AsArray().Deserialize<List<PresenceDay>>(options);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM