【发布时间】:2022-01-19 12:01:42
【问题描述】:
从 AWS lambda 我得到这个 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"}]
我已经声明了一个类,我想反序列化从这个 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; }
}
我正在尝试这样做:
List<GetWesLotToGenerateReturn> sample = JsonSerializer.Deserialize<List<GetWesLotToGenerateReturn>>(lots);
我收到此错误:
The JSON value could not be converted to System.Collections.Generic.List`1[Service.App.Models.AdaptersModels.GetWesLotToGenerateReturn]. Path: $ | LineNumber: 0 | BytePositionInLine: 268.
如何正确地将 JSON 从列表反序列化为 C# 中的对象列表?
提前致谢!
【问题讨论】:
-
您给出的示例字符串只有 235 个字符,所以我怀疑这不是您实际反序列化的字符串。
-
(还不清楚您使用的是哪个框架 - 是
System.Text.Json.JsonSerializer还是Newtonsoft.Json.JsonSerializer?minimal reproducible example 真的会有所帮助...) -
我正在使用
System.Text.Json.JsonSerializer -
@JonSkeet 这个 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\\\"}]\""我只是想以更易读的格式显示它 -
如果 JSON 字符串实际上包含任何反斜杠,我会感到非常惊讶。我强烈怀疑这只是调试器执行转义。如果您可以按照我之前的要求提供minimal reproducible example,我们可以为您提供帮助……但在那之前,我怀疑任何人都可以做。
标签: c# json jsonserializer