Reputation: 510
I have one of these awful scenarios; My Api was working this morning, the JSON response was being deserialized to an array of myObject[] using the line of code below. Then I added a field to the database and an ApiKey to the API, I still get a solid response from the API but now the JsonSerializer.Deserialize won't work! The models still match (I'm only using the Id and Name).
I'm working in AWS with Lamda and RDS. The Api is being called from MAUI.
This line of code did work:
Clubs = JsonSerializer.Deserialize<ClubModel[]>(content, serializerOptions);
and of course now it doesn't!
The error is a fairly useless:
The JSON value could not be converted to Models.MYModel[]. Path: $ | LineNumber: 0 | BytePositionInLine: 1.
The JSON returned is still valid. (cropped here for brevity).
{
"statusCode": 200,
"body": "[{\"Id\":1002,\"Name\":\"Lucan \",\"Phone\":null,\"Www\":null,\"Address\":null,\"Description\":null,\"CountryId\":1,\"CrestPhoto\":null,\"Teams\":[]},{\"Id\":1003,\"Name\":\"Ballinteer\",\"Phone\":null,\"Www\":null,\"Address\":null},
"isBase64Encoded": false
}
My model is a fairly simple poco with the Json annotations:
public class ClubModel
{
[JsonPropertyName("Id")]
public int Id { get; set; }
[JsonPropertyName("Name")]
public string? Name { get; set; }
}
The only questionable thing I see here is the "statusCode" at the start?, was that always there??
"statusCode": 200,
Looking at the old code that I'm working from this isn't there, it just goes straigt into the array of objects.
Same for the "isBase64Encoded": false
on the end. That's not in the old code.
I don't believe that I need the code:
"statusCode": 200,
"body":
So I think the 'StatusCode' is interfering with the decoding but I don't know how to fix it. I have a feeling I flagged something on but I can't see it. Or how can I get past in on the calling side? response.body??
Upvotes: 0
Views: 49