from the AWS lambda I get this JSON string:
[{"Id":19162,"LotId":21243,"LotNumber":"H6469","LotType":20,"ConfirmationStatus":0,"Date":"2016-02-17T10:51:06.757"},{"Id":19163,"LotId":21244,"LotNumber":"H6469a","LotType":20,"ConfirmationStatus":0,"Date":"2016-02-17T10:51:19.933"}]
I have declared a class to which I want to deserialize the data received from this API.
public class GetWesLotToGenerateReturn
{
public long Id { get; set; }
public long LotId { get; set; }
public string LotNumber { get; set; }
public int LotType { get; set; }
public int ConfirmationStatus { get; set; }
public DateTime Date { get; set; }
}
I'm trying to do this:
List<GetWesLotToGenerateReturn> sample = JsonSerializer.Deserialize<List<GetWesLotToGenerateReturn>>(lots);
And I receive this error:
The JSON value could not be converted to System.Collections.Generic.List`1[Service.App.Models.AdaptersModels.GetWesLotToGenerateReturn]. Path: $ | LineNumber: 0 | BytePositionInLine: 268.
How can I properly deserialize JSON from a list to a list of objects in C#?
Thanks in advance!
CodePudding user response:
your json
json= "\"[{\\\"Id\\\":19162,\\\"LotId\\\":21243,\\\"LotNumber\\\":\\\"H6469\\\",\\\"LotType\\\":20,\\\"ConfirmationStatus\\\":0,\\\"Date\\\":\\\"2016-02-17T10:51:06.757\\\"},{\\\"Id\\\":19163,\\\"LotId\\\":21244,\\\"LotNumber\\\":\\\"H6469a\\\",\\\"LotType\\\":20,\\\"ConfirmationStatus\\\":0,\\\"Date\\\":\\\"2016-02-17T10:51:19.933\\\"}]\"";
you serialized your json twice and it needs to be fixed at first
json=JsonConvert.DeserializeObject<string>(json);
after this I deserialized it using both Serializer (MS and Newtonsoft) and everything is ok
var jd = JsonConvert.DeserializeObject<List<GetWesLotToGenerateReturn>>(json);
var jdm = System.Text.Json.JsonSerializer.Deserialize<List<GetWesLotToGenerateReturn>>(json);