【问题标题】:C# class representation for this json此 json 的 C# 类表示
【发布时间】:2021-03-18 07:02:26
【问题描述】:

我需要一点帮助来创建 C# 类来映射来自 FCM 令牌信息 api (https://developers.google.com/instance-id/reference/server#get_information_about_app_instances) 的 json 响应:

{
   "application": "com.chrome.windows",
   "subtype": "wp:www.mydomain.com/#A1249A346-7458-45BB-A0F2-2AC4856BB-V2",
   "scope": "*",
   "authorizedEntity": "8212312155",
   "rel": {
      "topics": {
         "topic1": {
            "addDate": "2020-12-06"
         }
      }
   },
   "platform": "BROWSER"
}

其中 topic1 不是属性名称,而是一个值,而属性名称 topics 包含主题列表。

我其实不知道如何在上面的json中表示topics

【问题讨论】:

  • public Dictionary<string, Topic> Topics {get;set;}
  • Visual Studio 粘贴菜单 => 选择性粘贴 -> 将 Json 粘贴为类
  • 如果您想更好地命名属性,您可以随意更改它们,并在属性上方添加 [JsonProperty("topic1")]。所以序列化不会出现任何错误

标签: c# .net asp.net-core asp.net-core-2.0 asp.net-core-3.1


【解决方案1】:

是字典格式。

public Dictionary<string, Topic> Topics {get;set;}

类如下:

public class Root
{
    public string application { get; set; }
    public string subtype { get; set; }
    public string scope { get; set; }
    public string authorizedEntity { get; set; }
    public Rel rel { get; set; }
    public string platform { get; set; }
}

public class Rel
{
    public Dictionary<string, Topic> topics { get; set; }
}

public class Topic
{
    public string addDate { get; set; }
}

上面的json可以从下面的模型序列化:

var root = new Root
{
    application = "com.chrome.windows",
    subtype = "wp:www.mydomain.com/#A1249A346-7458-45BB-A0F2-2AC4856BB-V2",
    scope = "*",
    authorizedEntity = "8212312155",
    rel = new Rel
    {
        topics = new Dictionary<string, Topic>
        {
            { 
                "topic1", 
                new Topic
                { 
                    addDate = "2020-12-06" 
                }
            }
        }
    },
    platform = "BROWSER"
};

【讨论】:

    【解决方案2】:
        public class Topic1    {
            public string addDate { get; set; } 
        }
    
        public class Topics    {
            public Topic1 topic1 { get; set; } 
        }
    
        public class Rel    {
            public Topics topics { get; set; } 
        }
    
        public class Root    {
            public string application { get; set; } 
            public string subtype { get; set; } 
            public string scope { get; set; } 
            public string authorizedEntity { get; set; } 
            public Rel rel { get; set; } 
            public string platform { get; set; } 
        }
    

    https://json2csharp.com/

    如果您确定主题包含唯一的主题名称,请使用字典,如果不确定,则使用列表

    【讨论】:

    • 主题包含唯一的主题名称,但主题可以包含任意数量的主题。所以,我们不能有像 Topic1 这样的类
    • @Ivan 我投了反对票。使用在线工具很容易将一些给定的 Json 字符串 1:1 转换,但它不能回答问题。主题可以包含多个命名项,不能直接表示。它实际上需要一个 Dictionary 项。它会有一个用于上述 Json 字符串的条目,并带有一个名为“Topic1”的键。 Paul Michaels 写了一个很好的教程如何解析你的 Json 字符串和添加字典项。对于每个主题:pmichaels.net/2016/12/26/…
    • 稍后我明白了 Ahmed 想要什么 :) 当然使用字典是个好主意,但我没有时间更新代码。所以你的减号是正确的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-20
    • 2014-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-19
    相关资源
    最近更新 更多