2

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!

5
  • 3
    The sample string you've given is only 235 characters, so I suspect that's not the string you're actually deserializing. Commented Dec 16, 2021 at 11:35
  • 1
    (It's also unclear which framework you're using - is that System.Text.Json.JsonSerializer, or Newtonsoft.Json.JsonSerializer? A minimal reproducible example would really help...) Commented Dec 16, 2021 at 11:36
  • I'm using System.Text.Json.JsonSerializer Commented Dec 16, 2021 at 11:40
  • @JonSkeet this JSON string is actually: "\"[{\\\"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 just wanted to show it in more readable format Commented Dec 16, 2021 at 11:42
  • I'd be very surprised if the JSON string actually included any backslashes. I strongly suspect that's just the debugger performing escaping. If you could provide a minimal reproducible example as I requested before, we could help you... but until then, I doubt that there's much anyone can do. Commented Dec 16, 2021 at 11:56

1 Answer 1

7

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\\\"}]\"";

your json was serialized twice (probably by using JSON.stringify) and 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);
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much! I actually didn't realised that I serialize object in lambda before sending it and in my endpoint. I've just deleted serializing in lambda and it started to work! :)
@MateoSkyline: Note that if you'd included a minimal reproducible example from the start, this would have been easy to solve. The very first statement in the question is incorrect - because you're not getting the JSON you claim to be getting. Please help us to help you in future.
for beautify json you can use codebeautify.org/jsonviewer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.