【发布时间】: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