【问题标题】:SlackAPI - payload deserialization with dynamic json propertiesSlackAPI - 具有动态 json 属性的有效负载反序列化
【发布时间】:2021-08-11 10:20:55
【问题描述】:

我正在开发我的 SlackAPI 应用程序。

我已经创建了模态窗口,当来自这个模态的表单被提交时 - 我收到了一个看起来像这样的 JSON 有效负载(不重要的 JSON 部分被删除)

{
   "type":"view_submission",
   "view":{
      "state":{
         "values":{
            "IGhn":{
               "e5+":{
                  "type":"static_select",
                  "selected_option":{
                     "text":{
                        "type":"plain_text",
                        "text":"2021\/8",
                        "emoji":true
                     },
                     "value":"2"
                  }
               }
            }
         }
      },

此有效负载由我的端点正确处理-除了部分-状态-值,它们是空值...

我有这样定义的模型:

public class View
{
...
[JsonProperty("state")]
public State State { get; set; }
...
}

public class State
{
[JsonProperty("values")]
public Dictionary<string, Values> Values { get; set; }
}

public class Values
{
[JsonProperty()] // <- This is the problem
public Dictionary<string, DynamicValue> something { get; set; }
}

public class DynamicValue
{
[JsonProperty("selectedOptions")] // <- This is null
public Dictionary<string, SelectedOption> SelectedOptions { get; set; }
}

如果值(属性名称)是静态的,那没关系,但问题是:IGhn / e5+ 是动态变化的 - 所以反序列化不起作用...

必须说整个 JSON 已正确反序列化,但我无法反序列化 IGhn 下的其余部分(我什至不知道如何为它创建类……反序列化器知道该怎么做…… .)

【问题讨论】:

    标签: json deserialization slack-api json-deserialization asp.net-core-5.0


    【解决方案1】:

    好的 - 我找到了解决方案:

    我改变了班级状态:

        public class State
            {
                [JsonProperty("values")]
                public Dictionary<string, Dictionary<string, ParentForSelectedOption>> Values { get; set; }
            }
    

    然后 Object ParentForSelectedOption 以常规方式反序列化 - 因此这些动态名称通过使用字典解决 这些可以删除:

    public class Values
    {
    [JsonProperty()] // <- This is the problem
    public Dictionary<string, DynamicValue> something { get; set; }
    }
    
    public class DynamicValue
    {
    [JsonProperty("selectedOptions")] // <- This is null
    public Dictionary<string, SelectedOption> SelectedOptions { get; set; }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多