【问题标题】:Convert JSON object to Model in .NET在 .NET 中将 JSON 对象转换为模型
【发布时间】: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


【解决方案1】:

您在 C# 代码中将 AgreementNumberClientNumber 定义为字符串,但此属性是 json 中的数字,因此您必须将其定义为 long。

另外一点是,您不需要对 DebtConfirmation 类进行包装。将您的 json 分解为 ICollectionIList 或只是 ListDebtConfirmation 对象。

我使用quicktype.io 从您提供的 json 示例中检索 C# 类。这对于那些不想为其 JSON 字符串手动生成模型的人非常有帮助。

这是代码示例。

输出是:

789
866
using System.Text.Json;
using System.Text.Json.Serialization;

string json = "[\n  {\n    \"batchId\": 789,\n    \"debtId\": 1841,\n    \"dateAdded\": \"2021-07-27T16:01:39.41\",\n    \"debtCategoryId\": 2,\n    \"agreementNumber\": 78262155,\n    \"clientNumber\": 1068055,\n    \"clientName\": \"Client Two\"\n  },\n  {\n    \"batchId\": 866,\n    \"debtId\": 1918,\n    \"dateAdded\": \"2021-08-25T14:47:18.13\",\n    \"debtCategoryId\": 2,\n    \"agreementNumber\": 1000140792,\n    \"clientNumber\": 11213287,\n    \"clientName\": \"Client One\"\n  }\n]";
var data = JsonSerializer.Deserialize<ICollection<DebtConfirmation>>(json);
foreach (DebtConfirmation current in data)
{
    Console.WriteLine(current.BatchId);
}


public partial class DebtConfirmation 
{
    [JsonPropertyName("batchId")]
    public long BatchId { get; set; }

    [JsonPropertyName("debtId")]
    public long DebtId { get; set; }

    [JsonPropertyName("dateAdded")]
    public DateTimeOffset DateAdded { get; set; }

    [JsonPropertyName("debtCategoryId")]
    public long DebtCategoryId { get; set; }

    [JsonPropertyName("agreementNumber")]
    public long AgreementNumber { get; set; }

    [JsonPropertyName("clientNumber")]
    public long ClientNumber { get; set; }

    [JsonPropertyName("clientName")]
    public string ClientName { get; set; }
}

【讨论】:

    【解决方案2】:

    您可以使用@GeorgyTarasov 的答案。但这绝对不是最简单的选择。

    您可以简单地使用JsonNamingPolicy.CamelCase

    那你就干脆……

    var options = new JsonSerializerOptions
    {
        DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
    };
    var ret = JsonSerializer.Deserialize<DebtConfirmation>(payload, options);
    

    如果您使用的是 AspNet Core,您可以在此处注册该选项

    services.AddControllers()
        .AddJsonOptions(options =>
        {
            options.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      • 1970-01-01
      • 1970-01-01
      • 2022-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多