【问题标题】:API IActionResult resultAPI IActionResult 结果
【发布时间】:2020-12-29 12:09:47
【问题描述】:

我正在创建一个 .net 核心 API,但我收到了奇怪的响应。

在内部 API 中,我询问另一个外部 API,并且在从外部 API 获取结果时使用 JsonConvert.DeserializeObject。 我能够反序列化到我自己的类,但是当我在 Postman 中向我的 API 发出请求时,我得到了用于反序列化的字段。

    public class InvoiceHistoryResponse
    {
        [JsonProperty("results")]
        public IEnumerable<Invoice> Invoices { get; set; }
    }

    public class Invoice
    {
        [JsonProperty("properties")]
        public InvoiceProperty InvoiceProperty { get; set; }

        [JsonProperty("nodes")]
        public InvoiceFile InvoiceFiles { get; set; }
    }

    public class InvoiceFile
    {
        [JsonProperty("file")]
        public string Pdf { get; set; }
    }

    public class InvoiceProperty
    {
        [JsonProperty("invoicenumber")]
        public string InvoiceNumber { get; set; }

        [JsonProperty("totalsum")]
        public decimal InvoiceTotalSum { get; set; }

        [JsonProperty("issuedate")]
        public DateTime InvoiceIssueDate { get; set; }
    }

这是我反序列化时的代码:

var invoiceHistoryResponse = JsonConvert.DeserializeObject<InvoiceHistoryResponse>(content);
return invoiceHistoryResponse.Invoices;

这是 Postman 的结果:

[
    {
        "properties": {
            "invoicenumber": "45293556",
            "totalsum": 1239.55,
            "issuedate": "2000-04-08T00:00:00+02:00"
        },
        "nodes": {
            "file": "https://file.pdf"
        }
    },
    {
        "properties": {
            "invoicenumber": "52095938",
            "totalsum": 989.33,
            "issuedate": "2001-08-18T00:00:00+02:00"
        },
        "nodes": {
            "file": "https://file.pdf"
        }
    },
    {
        "properties": {
            "invoicenumber": "28842180",
            "totalsum": 1504.38,
            "issuedate": "2005-06-06T00:00:00+02:00"
        },
        "nodes": {
            "file": "https://file.pdf"
        }
    }
]

但是响应应该是这样的:

[
    {
        "InvoiceProperty": {
            "InvoiceNumber": "45293556",
            "InvoiceTotalSum": 1239.55,
            "InvoiceIssueDate": "2000-04-08T00:00:00+02:00"
        },
        "InvoiceFiles": {
            "Pdf": "https://file.pdf"
        }
    },
    {
        "InvoiceProperty": {
            "InvoiceNumber": "52095938",
            "InvoiceTotalSum": 989.33,
            "InvoiceIssueDate": "2001-08-18T00:00:00+02:00"
        },
        "InvoiceFiles": {
            "Pdf": "https://file.pdf"
        }
    },
    {
        "InvoiceProperty": {
            "InvoiceNumber": "28842180",
            "InvoiceTotalSum": 1504.38,
            "InvoiceIssueDate": "2005-06-06T00:00:00+02:00"
        },
        "InvoiceFiles": {
            "Pdf": "https://file.pdf"
        }
    }
]

在 Controller 中,端点如下所示:

[Produces(typeof(IEnumerable<Invoice>))]
public async Task<IActionResult> GetAllInvoices([FromRoute] string customerId)
{
    var invoices = await _invoiceService.GetCustomerInvoice();
    return Ok(invoices);
}

知道为什么我会得到这个奇怪的结果吗?

【问题讨论】:

  • [JsonProperty("results")] 覆盖属性名称...
  • 您可以使用 ActionResult&lt;IEnumerable&lt;Invoice&gt;&gt; 而不是 IActionResult,这 a) 可以更清楚地显示实际返回的内容,b) 您可以省略 Ok 包装器,只需执行 return invoices;,c) 您可以省略 ProducesAttribute,因为返回类型已经指定了这一点。

标签: c# json .net .net-core json.net


【解决方案1】:

JsonProperty 用于在将属性序列化为 JSON 时更改属性的名称。 所以基本上当您使用JsonProperty 属性时,您的响应将从JsonProperty 中获取名称,但是在反序列化时,它会识别JsonProperty 名称并为属性分配正确的值。不仅是邮递员,所有显示简单响应的平台都会产生如下结果

[
    {
        "properties": {
            "invoicenumber": "45293556",
            "totalsum": 1239.55,
            "issuedate": "2000-04-08T00:00:00+02:00"
        },
        "nodes": {
            "file": "https://file.pdf"
        }
    },
    {
        "properties": {
            "invoicenumber": "52095938",
            "totalsum": 989.33,
            "issuedate": "2001-08-18T00:00:00+02:00"
        },
        "nodes": {
            "file": "https://file.pdf"
        }
    },
    {
        "properties": {
            "invoicenumber": "28842180",
            "totalsum": 1504.38,
            "issuedate": "2005-06-06T00:00:00+02:00"
        },
        "nodes": {
            "file": "https://file.pdf"
        }
    }
]

JsonProperty 仅在您打算在序列化对象时更改属性名称时使用。 如果您想在运行时修改属性名称,我已在其他问题中发布了答案。检查这个Set different property name for JSON at runtime

【讨论】:

  • JsonProperty [is] 仅在您打算在序列化对象时更改属性名称时使用 - 或者当然是在反序列化为不同名称时使用..
  • 但是来自外部 API 的响应具有我在类中指定的名称,这就是我放置 [JsonProperty] 的原因
  • 使用合约解析器,可以控制序列化和反序列化,看我回答中的链接
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-26
  • 2018-03-31
  • 2017-01-15
  • 2019-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多