【问题标题】:Azure Function Object Return not returning correct nested valuesAzure 函数对象返回未返回正确的嵌套值
【发布时间】:2021-11-11 00:23:27
【问题描述】:

我有一个 API 预期会返回如下结果(其中包含从 Cosmos DB 获取的数据)

public class ConsumerResponse
    {
        public string ObjectId { get; set; }
        public string ConsumerType { get; set; }
        public object Data { get; set; }
    }

我正在使用邮递员来测试这个 API。下面是返回 CustomerResponse 对象的 API 函数

[HttpGet]
    [Route("{container}/{consumerType}/{objectId}")]
    [ProducesResponseType(typeof(ConsumerResponse), (int)HttpStatusCode.OK)]
    [ProducesResponseType(typeof(ErrorResponse), (int)HttpStatusCode.BadRequest)]
    [ProducesResponseType(typeof(ErrorResponse), (int)HttpStatusCode.InternalServerError)]
    public async Task<ConsumerResponse> Get(string container , string consumerType, string objectId)
    {
        var response =  await _messageProcessingService.GetAsync(container, consumerType, objectId);
        return response;
    }

当我调试代码时,我可以看到映射到 CustomerResponse 类中每个属性的所有有效数据。 “数据”是嵌套对象。但是,当我在 Postman 中调用此 API 时,“Data”对象的嵌套值变为空,如下所示

{
    "objectId": "8bea9894-8685-46e4-8637-5614e624b05e",
    "consumerType": "Patient",
    "data": {
        "name": [
            [
                []
            ],
            [
                []
            ],
            [
                []
            ],
            [
                []
            ]
        ],
        "gender": [],
        "birthDate": [],
        "countryCode": [],
        "isActive": [],
        "consumerConsent": [],
        "accountStatus": [
            [
                [
                    []
                ]
            ]
        ]
    }
}

但是,如果我将“数据”的数据类型更改为字符串,那么我会得到如下结果

{
    "objectId": "8bea9894-8685-46e4-8637-5614e624b05e",
    "consumerType": "Patient",
    "data": "{\r\n  \"name\": {\r\n    \"use\": \"Official\",\r\n    \"surname\": \"Dope\",\r\n    \"firstName\": \"Johny\",\r\n    \"prefix\": \"Mr.\"\r\n  },\r\n  \"gender\": \"Male\",\r\n  \"birthDate\": \"1991-02-02\",\r\n  \"countryCode\": \"IN\",\r\n  \"isActive\": false,\r\n  \"consumerConsent\": true,\r\n  \"accountStatus\": [\r\n    {\r\n      \"startDate\": \"2021-09-08T09:27:21.1946786+10:00\"\r\n    }\r\n  ]\r\n}"
}

当我使用 Object 数据类型时,如何获取嵌套结构的值?

-艾伦-

【问题讨论】:

  • 这里没有很多工作要做。此外,您使用public object Data 进行的整个对象事情非常可疑
  • 你能把 Object 改成该类型对象的 Class 试试看吗?
  • 那么,只使用字符串而不是对象呢?这似乎是一个数据转换问题,这里没有太多选择可以尝试。

标签: c# .net-core postman azure-cosmosdb asp.net-core-webapi


【解决方案1】:

您可以将所有值作为数据类型作为字符串获取,因为它获取键值对因此您可以在此处查看以转换为json check here

在 Azure 函数中,您需要添加对 NewtonSoft.JSON 的引用

Newtonsoft.Json 库是 special case。您可以通过在脚本文件的顶部添加它来包含它:

#r "Newtonsoft.Json"

using Newtonsoft.Json;

并添加以下代码行以获取 json 嵌套值。

var response = await client.GetAsync("<url>");

var json = await response.Content.ReadAsStringAsync();

var obj= JsonConvert.DeserializeObject<"Type">(json);

更多信息请参考这里link 1 & link 2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 2019-06-11
    • 1970-01-01
    • 2021-01-23
    • 1970-01-01
    相关资源
    最近更新 更多