首页 > 解决方案 > 添加项目后返回 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"

标签: c#.net-core

解决方案


根据我的收集,您正在尝试向从您的 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"
}

推荐阅读