.Net 6 API 不返回嵌套列表对象
.Net 6 API not returning nested list objects
我们正在将我们的 API 从 .net 4.8 库移动到 .Net 6。我遇到了一个奇怪的问题,其中 none 的嵌套对象(成本)正在被序列化。
我尝试在启动时添加 using 但我的 api 无法使用此选项。我什至看不到控制器被这个选项击中。
Newtonsoft.Json.Serialization;
...............
services.AddControllers().AddNewtonsoftJson();
或
`services.AddControllers().AddNewtonsoftJson(options =>`
`{`
`options.SerializerSettings.ContractResolver = new DefaultContractResolver();`
`options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;`
`});`
services.AddControllers().AddJsonOptions(o =>
{
o.JsonSerializerOptions.MaxDepth = 4;
o.JsonSerializerOptions.PropertyNamingPolicy = null;
o.JsonSerializerOptions.DictionaryKeyPolicy = null;
});
我错过了什么?
我希望格式如下
`{
"Status": 200,
"Message": "Processed Successfully",
"Description": null,
"Errors": null,
"ResponseObject": {
"Costs": [
{
"Component": "xxx",
"Key1": "123",
"Key2": null,
"Key3": null,
"ProductCode": "x",
"From": "2021-08-05T00:00:00",
"To": "2021-08-05T23:59:59",
"Value": "1584",
"CurrencyCode": null,
"UnitOfMeasure": null
},
{
"Component": "X",
"Key1": "123",
"Key2": null,
"Key3": null,
"ProductCode": "Y",
"From": "2021-08-05T00:00:00",
"To": "2021-08-05T23:59:59",
"Value": "3131",
"CurrencyCode": null,
"UnitOfMeasure": null
}
]
}`
C#对象如下:
public class Response
{
public int Status { get; set; }
public string Message { get; set; }
public string Description { get; set; }
public object Errors { get; set; }
public object ResponseObject { get; set; }
}
下面是用于填充对象的代码:
var costsVM = new CostsVM();
var response = new Response();
costsVM.Costs.AddRange(costs);
response.ResponseObject = costsVM;
response.Status = 200;
response.Message = "Processed Successfully";
response.Description = null;
return Ok(response);
Cost VM定义如下:
public class CostsVM
{
public List<CostsDTO> Costs = new List<CostsDTO>();
}
public class CostsDTO
{
public string Component { get; set; }
public string? Key1 { get; set; }
public string? Key2 { get; set; }
public string? Key3 { get; set; }
public string ProductCode { get; set; }
public DateTime From { get; set; }
public DateTime To { get; set; }
public string Value { get; set; }
public string? CurrencyCode { get; set; }
public string? UnitOfMeasure { get; set; }
}
我似乎找到了解决方法。
我需要在 JsonSerializer 中启用 IncludeFields
services.AddMvc().AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = null;
options.JsonSerializerOptions.MaxDepth = 64;
options.JsonSerializerOptions.IncludeFields = true;
});
我们正在将我们的 API 从 .net 4.8 库移动到 .Net 6。我遇到了一个奇怪的问题,其中 none 的嵌套对象(成本)正在被序列化。
我尝试在启动时添加 using 但我的 api 无法使用此选项。我什至看不到控制器被这个选项击中。
Newtonsoft.Json.Serialization;
...............
services.AddControllers().AddNewtonsoftJson();
或
`services.AddControllers().AddNewtonsoftJson(options =>`
`{`
`options.SerializerSettings.ContractResolver = new DefaultContractResolver();`
`options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;`
`});`
services.AddControllers().AddJsonOptions(o =>
{
o.JsonSerializerOptions.MaxDepth = 4;
o.JsonSerializerOptions.PropertyNamingPolicy = null;
o.JsonSerializerOptions.DictionaryKeyPolicy = null;
});
我错过了什么?
我希望格式如下
`{
"Status": 200,
"Message": "Processed Successfully",
"Description": null,
"Errors": null,
"ResponseObject": {
"Costs": [
{
"Component": "xxx",
"Key1": "123",
"Key2": null,
"Key3": null,
"ProductCode": "x",
"From": "2021-08-05T00:00:00",
"To": "2021-08-05T23:59:59",
"Value": "1584",
"CurrencyCode": null,
"UnitOfMeasure": null
},
{
"Component": "X",
"Key1": "123",
"Key2": null,
"Key3": null,
"ProductCode": "Y",
"From": "2021-08-05T00:00:00",
"To": "2021-08-05T23:59:59",
"Value": "3131",
"CurrencyCode": null,
"UnitOfMeasure": null
}
]
}`
C#对象如下:
public class Response
{
public int Status { get; set; }
public string Message { get; set; }
public string Description { get; set; }
public object Errors { get; set; }
public object ResponseObject { get; set; }
}
下面是用于填充对象的代码:
var costsVM = new CostsVM();
var response = new Response();
costsVM.Costs.AddRange(costs);
response.ResponseObject = costsVM;
response.Status = 200;
response.Message = "Processed Successfully";
response.Description = null;
return Ok(response);
Cost VM定义如下:
public class CostsVM
{
public List<CostsDTO> Costs = new List<CostsDTO>();
}
public class CostsDTO
{
public string Component { get; set; }
public string? Key1 { get; set; }
public string? Key2 { get; set; }
public string? Key3 { get; set; }
public string ProductCode { get; set; }
public DateTime From { get; set; }
public DateTime To { get; set; }
public string Value { get; set; }
public string? CurrencyCode { get; set; }
public string? UnitOfMeasure { get; set; }
}
我似乎找到了解决方法。
我需要在 JsonSerializer 中启用 IncludeFields
services.AddMvc().AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = null;
options.JsonSerializerOptions.MaxDepth = 64;
options.JsonSerializerOptions.IncludeFields = true;
});