c# - 添加项目后返回 null 的列表
问题描述
我正在将项目添加到列表中,但是当我返回列表时它是空的,不确定发生了什么。从 net core 2.2 迁移到 3.1 后观察此行为。
如果我删除它就可以(string guid, string type)
了List
public async Task<IActionResult> GetLatestProducts()
{
var httpResp = new List<(LatestProducts.RootObject, string guid, string type)>();
var request = new HttpRequestMessage(HttpMethod.Get, "p_spring-cloud-services/releases/latest");
var response = await _client_deployed_prod_update.SendAsync(request);
var json = await response.Content.ReadAsStringAsync();
LatestProducts.RootObject model = JsonConvert.DeserializeObject<LatestProducts.RootObject>(json);
if (model != null)
{
httpResp.Add((model, "p_spring-cloud-services-9f414709158c9309f5e3", "p_spring-cloud-services"));
}
return Ok(httpResp);
}
班级:
public class LatestProducts
{
public class RootObject
{
public int id { get; set; }
public Version version { get; set; }
public string release_type { get; set; }
public string release_date { get; set; }
}
}
我从请求中得到的响应:
{
"id": 558725,
"version": {
"major": 3,
"minor": 1,
"build": 5,
"revision": -1,
"majorRevision": -1,
"minorRevision": -1
},
"release_type": "Maintenance Release",
"release_date": "2020-01-22"
}
我希望列表看起来像什么:
{
"id": 558725,
"version": {
"major": 3,
"minor": 1,
"build": 5,
"revision": -1,
"majorRevision": -1,
"minorRevision": -1
},
"release_type": "Maintenance Release",
"release_date": "2020-01-22",
}
"guid": "p_spring-cloud-services-9f414709158c9309f5e3",
"type": "p_spring-cloud-services"
解决方案
根据我的收集,您正在尝试向从您的 json 中获取数据的类添加另外两个属性。问题是,你不能有一个只有值的 json,你也需要密钥。以下过程在模型添加到响应对象时使用键:“请求”。
您可以有两个单独的类,一个用于您的模型,一个用于您的响应。与此类似,您拥有包含模型和响应的RootObject
public class Version
{
public int major { get; set; }
public int minor { get; set; }
public int build { get; set; }
public int revision { get; set; }
public int majorRevision { get; set; }
public int minorRevision { get; set; }
}
public class RootObject
{
public int id { get; set; }
public Version version { get; set; }
public string release_type { get; set; }
public string release_date { get; set; }
}
public class Response
{
public RootObject request { get; set; }
public string guid { get; set; }
public string type { get; set; }
}
并主要使用这些类,
var model = JsonConvert.DeserializeObject<RootObject>(json);
var httpResp = new Response
{
guid = "p_guid",
type = "p_type",
request = model
};
Console.WriteLine(JsonConvert.SerializeObject(httpResp, Formatting.Indented));
输出
上面产生以下json:
{
"request": {
"id": 558725,
"version": {
"major": 3,
"minor": 1,
"build": 5,
"revision": -1,
"majorRevision": -1,
"minorRevision": -1
},
"release_type": "Maintenance Release",
"release_date": "2020-01-22"
},
"guid": "p_guid",
"type": "p_type"
}
推荐阅读
- postgresql - Automigrate in GORM database adds unwanted fields to SQL table
- arrays - 如何将地图存储到 Firestore 数组
- ios - Rest API 应该只添加新记录,或者应该通过删除来更新记录
- json - 使用 curl 将团队添加到存储库的正确方法
- batch-processing - 我们可以对批处理进行窗口化吗?
- clang-static-analyzer - 如何匹配指向空对象的指针?
- javascript - 使用 javaScript 循环对象数组的最佳方法是什么?
- python - 使用 == 符号时的 if 和语句混淆
- amazon-web-services - 如何解决 AWS 爬虫在双引号内拆分逗号并破坏数据目录的问题?
- node.js - 为什么我的表单数据在 Node.js/Express 中被截断?