【问题标题】:Serializing to model - serialize node to choice of two objects序列化到模型 - 序列化节点以选择两个对象
【发布时间】:2020-12-16 13:30:30
【问题描述】:

我有一个如下结构的 JSON 文件。

{
    title: {
        title: "",
        episode_title: ""
    },
    series: {
        season: "",
        episode: ""
    }
    content_description: {
        ???
    }
}

,其中 content_description 可能具有以下格式之一:

content_description: {
    language: "",
    short_synapsis: "",
    medium_synopsis: "",
    long_synopsis: "",
    oneliner: ""
}

OR

content_description: {
    text: "",
    category: "",
    level: "", 
    source: ""
}

我想使用 Newtonsoft Json.NET JsonConvert.DeserializeObject(string value) 方法将其序列化为 C# 模型。到目前为止,我已经创建了以下 content_description 模型:

public class ContentDescriptions
{
    public string language { get; set; }
    public string short_synopsis { get; set; }
    public string medium_synopsis { get; set; }
    public string long_synopsis { get; set; }
    public string short_season_synopsis { get; set; }
    public string medium_season_synopsis { get; set; }
    public string long_season_synopsis { get; set; }
    public string short_episode_synopsis { get; set; }
    public string medium_episode_synopsis { get; set; }
    public string long_episode_synopsis { get; set; }
    public string oneliner { get; set; }
    public string text { get; set; }
    public string category { get; set; }
    public string level { get; set; }
    public string source { get; set; }
}

这可行,但是有没有办法将具有一个名称的元素序列化为两个模型之一,具体取决于其结构?例如,在我的根模型中有两个 content_description 模型字段并填充其中一个而将另一个留空?

【问题讨论】:

  • 不能将 JSON 序列化为任何东西,因为 JSON 本身就是序列化的结果。不清楚,你的确切问题是什么。您有没有按预期工作的代码示例?
  • 尝试将结果转换为匿名对象或动态,然后查找在另一个模型上不重复的字段之一,然后决定选择哪个模型进行转换。
  • @Aegonek 请也与我们分享您的 json 文件。另外请尝试与我们分享您是如何尝试解决此问题的以及您在哪里卡住了。

标签: c# json json.net


【解决方案1】:

容器类会起作用吗?

class MyAmazingClass {
  ContentDescriptionsA A {get; set;}
  ContentDescriptionsB B {get; set;}
}

然后你可以(反)序列化这个类的对象。

【讨论】:

    【解决方案2】:

    如果我从您有限的信息中正确理解,您希望得到一个可以是 A 或 B 的 json 字符串。您对 B 不感兴趣,因为它可以用空值或空值实例化。

    在这种情况下,您可以每次都使用对象 A (ContentDescriptionsA) 进行反序列化。 A的内容的JsonString通过时会填充该类的属性,如果B的对象通过则属性将为null。

    这是一个简单的控制台应用程序,您可以重新构建它来玩转这个概念。 (旁注:您也可以创建一个包含两个 JsonString 的所有属性的类,但随后 B 的属性也将被填充)

    using System.Text.Json;
    
    namespace TestJson
    {
    class Program
    {
        static void Main(string[] args)
        {
            var a = new ContentDescriptionsA() { language = "NederlandsA"};
            var b = new ContentDescriptionsB() { text = "TextB" };
    
            var jsonstringA = JsonSerializer.Serialize(a);
            var jsonstringB = JsonSerializer.Serialize(b);
    
    
            var deserializeA = JsonSerializer.Deserialize<ContentDescriptionsA>(jsonstringA); // properties of a FILLED, props of b NULL
            var deserializeB = JsonSerializer.Deserialize<ContentDescriptionsA>(jsonstringB); // all properties NULL
        }
    }
    
    public class ContentDescriptionsA
    {
        public string language { get; set; }
        public string short_synopsis { get; set; }
        public string medium_synopsis { get; set; }
        public string long_synopsis { get; set; }
        public string short_season_synopsis { get; set; }
        public string medium_season_synopsis { get; set; }
        public string long_season_synopsis { get; set; }
        public string short_episode_synopsis { get; set; }
        public string medium_episode_synopsis { get; set; }
        public string long_episode_synopsis { get; set; }
        public string oneliner { get; set; }
    }
    public class ContentDescriptionsB
    {
        public string text { get; set; }
        public string category { get; set; }
        public string level { get; set; }
        public string source { get; set; }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 2020-01-26
      • 2020-10-13
      • 1970-01-01
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多