【发布时间】:2022-01-25 05:27:54
【问题描述】:
我有这个从 API 返回的 JSON 对象:
[
{
"batchId": 789,
"debtId": 1841,
"dateAdded": "2021-07-27T16:01:39.41",
"debtCategoryId": 2,
"agreementNumber": 78262155,
"clientNumber": 1068055,
"clientName": "Client Two"
},
{
"batchId": 866,
"debtId": 1918,
"dateAdded": "2021-08-25T14:47:18.13",
"debtCategoryId": 2,
"agreementNumber": 1000140792,
"clientNumber": 11213287,
"clientName": "Client One"
}
]
我正在尝试将其转换为具有此结构的 C# 对象:
public class DebtConfirmationResponse
{
public List<DebtConfirmation> DebtConfirmations { get; set; }
}
DebtConfirmation 具有以下属性:
public class DebtConfirmation
{
public int BatchId { get; set; }
public int DebtId { get; set; }
public string DateAdded { get; set; }
public int DebtCategoryId { get; set; }
public string AgreementNumber { get; set; } = string.Empty;
public string ClientNumber { get; set; } = string.Empty;
public string ClientName { get; set; } = string.Empty;
}
我得到的错误是:
json 值无法转换为模型路径名称 $ linenumber 0 bytepositioninline 1
模型的设置方式有什么问题吗? 我还尝试将批次 ID 仅作为属性转换为相同的模型,但得到了相同的消息。
【问题讨论】:
-
您在 C# 代码中将
AgreementNumber、ClientNumber定义为字符串,但此属性是 json 中的数字,因此您必须将其定义为 longs -
谢谢,我确实只使用了 batchId 并得到了相同的消息。您认为模型层次结构设置正确吗?
标签: c# json .net asp.net-core jsonserializer