I'm a beginner in C# development and I need your help.. I am checking all the subject in Stackoverflow but I can't find and fix my problem. I received a JSON response from an API and I would like to transform the data in object.
------------ Here my JSON response :
{"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"}\],.......
------------ If I am Parsing it with 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": "",
......
------------ Here my 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; }
}
------------ Here my 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("--------------------");
}
------------ An I get that :
--------------------
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
--------------------
Hope you can help me
CodePudding user response:
Your JSON is broken. It's not JSON. Instead of \[
it should be [
and closing brackets likewise.
There is nothing a parser can do for you, if you feed it broken JSON.
You need to fix your data.
CodePudding user response:
your json is not valid, I fixed it at first
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);