【问题标题】:List returning null after adding item添加项目后返回 null 的列表
【发布时间】:2020-01-30 12:36:42
【问题描述】:

我正在将项目添加到列表中,但是当我返回列表时它是空的,不确定发生了什么。从 net core 2.2 迁移到 3.1 后观察此行为。

如果我在List 中删除(string guid, string type),它会起作用

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"

【问题讨论】:

  • 您期望响应数据是什么样的?
  • 为什么LatestProducts 是一个类?看起来应该是一个命名空间。
  • 欢迎来到 Stack Overflow。请通过tour 了解 Stack Overflow 的工作原理,并阅读How to Ask 以了解如何提高问题的质量。然后edit你的问题包含你的源代码作为minimal reproducible example,它可以被其他人编译和测试。
  • @Amy 它在命名空间下我只是在这里没有提到它。
  • @CaiusJard 我更新了问题

标签: c# .net-core


【解决方案1】:

据我所知,您正尝试向从您的 json 中获取数据的类添加另外两个属性。问题是,你不能有一个只有值的 json,你也需要密钥。以下过程在模型添加到响应对象时使用键:“请求”。

您可以有两个单独的类,一个用于您的模型,一个用于您的响应。与此类似,您的 RootObject 包含您的模型和 Response

    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"
}

【讨论】:

    【解决方案2】:

    如果要序列化,您将很难获得 List&lt;(object, string,string)&gt; 来生成您想要的输出,因为您已经说过您希望输出 JSON 的字符串名称为 guid 和 type,但是您的代码中没有此信息

    也许放弃列表(您想要的输出不包含集合)并尝试使用匿名类:

    return Ok(new { 
      model=model, 
      guid = "p_spring-cloud-services-9f414709158c9309f5e3", 
      type = "p_spring-cloud-services" 
    });
    

    请记住,您的模型数据最终将以 model 的名称引入 - 我不确定您是否会成功获得您指定的确切输出,因为它似乎不是有效的 Json (没有左大括号,模型数据没有名称,发布日期后尾随逗号,模型括号后缺少逗号,EOF 处缺少右括号)。这是我希望我的建议产生的结果:

    {
    "model": {
        "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"
    }
    

    如果您希望 "model": 成为其他名称,请在模型=模型左侧的匿名对象中将其命名为其他名称

    【讨论】:

      猜你喜欢
      • 2017-07-17
      • 2019-08-29
      • 1970-01-01
      • 2021-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多