【问题标题】:How to create multiple PropertyNames for one instance of class NewtonSoft [duplicate]如何为 NewtonSoft 类的一个实例创建多个 PropertyNames [重复]
【发布时间】:2021-01-12 21:21:34
【问题描述】:

所以,我尝试反序列化具有格式的 JSON 文件

"farmableitem": [
      {
        "@uniquename": "T1_FARM_CARROT_SEED",
        "@tier": "1",
        "@kind": "plant",
        "@weight": "0.1",
        "harvest": {
          "@growtime": "79200",
          "@lootlist": "T1_CARROT_LOOT",
          "@lootchance": "1",
          "@fame": "100",
          "seed": {
            "@chance": "0.0000",
            "@amount": "1"
          }
        }
      },      
      {
        "@uniquename": "T5_FARM_OX_BABY",
        "@tier": "5",
        "@kind": "animal",
        "@weight": "1.5",
        "grownitem": {
          "@uniquename": "T5_FARM_OX_GROWN",
          "@growtime": "504000",
          "@fame": "300",
          "offspring": {
            "@chance": "0.7867",
            "@amount": "1"
          }
        }
]

如您所见,“收获”和“生长物”以及“种子”和“后代”基本相同...... 所以我没有理由创建两个不同的类。

我知道,如果您想为特定字段提供两个不同的 PropertyName,您可以执行以下操作:

    [JsonProperty("first")]
    public string Test { get; set; }
    [JsonProperty("second")]
    private string _test { set { Test = value; } }

但是,我不确定我是否可以对课程做同样的事情......至少我找不到解决方案。

所以,长话短说,我希望一个类有两个不同的 JsonPropertyName。有可能吗?

【问题讨论】:

  • 您可以创建一个包含通用属性的基类,也可以重命名您的 json 结构以对每个通用对象使用相同的名称。后者更容易。如果正确表示您的 json 结构,那么拥有大量类并没有错。
  • 只有offspringseed 相同。其他属性有不同的属性,而不仅仅是不同的名称。
  • 我会在对象上创建两个属性,harvestgrownitem 都指向同一个支持字段
  • @DavidL 等等,重命名 json 结构是什么意思?
  • @PanagiotisKanavos 属性的名称相同 => 类中的字段名称也将相同...可以预期属性将具有不同的值

标签: c# json.net deserialization json-deserialization


【解决方案1】:

长话短说...我修复了它,但是,我的解决方案看起来像一个 F*** 创可贴...所以,如果您对如何优化我的 sn-p 有任何建议写好了,非常感谢您的帮助!

        public class Item {
            [JsonProperty(PropertyName = "@uniquename")]
            public string UniqueName { get; set; }

            [JsonProperty(PropertyName = "@tier")]
            public string Tier { get; set; }

            [JsonProperty(PropertyName = "@weight")]
            public string Weight { get; set; }

            [JsonProperty(PropertyName = "@shopcategory")]
            public string Category { get; set; }

            [JsonProperty(PropertyName = "@shopsubcategory1")]
            public string SubCategory { get; set; }
        }

        public class FarmableItem : Item
        {
            [JsonProperty(PropertyName = "@kind")]
            public string Kind { get; set; }

            private class HarvestModel
            {
                [JsonProperty(PropertyName = "@growtime")]
                public string GrowTime { get; set; }

                [JsonProperty(PropertyName = "@lootlist")]
                public string UniqueName { get; set; }

                [JsonProperty(PropertyName = "@fame")]
                public string Fame { get; set; }    

                public class HarvestedItemModel
                {
                    [JsonProperty(PropertyName = "@chance")]
                    public string SuccessRate { get; set; }

                    [JsonProperty(PropertyName = "@amount")]
                    public string Amount { get; set; }
                }

                public class GrownItemModel : HarvestedItemModel {}

                [JsonProperty(PropertyName = "seed")]
                public HarvestedItemModel HarvestedItem = new HarvestedItemModel();

                [JsonProperty(PropertyName = "offspring")]
                public GrownItemModel GrownItem = new GrownItemModel();

            }

            private class GrowModel : HarvestModel {}

            [JsonProperty("harvest")]
            private HarvestModel Harvest = new HarvestModel();

            [JsonProperty("grownitem")]
            private GrowModel Grow = new GrowModel();

            public string GrowTime => Harvest.UniqueName == null ? Grow.GrowTime : Harvest.GrowTime;
            public string Loot => Harvest.UniqueName == null ? Grow.UniqueName : Harvest.UniqueName;
            public string Fame => Harvest.UniqueName == null ? Grow.Fame : Harvest.Fame;

            public string SuccessChance => Harvest.UniqueName == null ? Grow.GrownItem.SuccessRate : Harvest.HarvestedItem.SuccessRate;
            public string Amount => Harvest.UniqueName == null ? Grow.GrownItem.Amount : Harvest.HarvestedItem.Amount;
        }

我的逻辑 => UniqueName 总是存在的,所以我们可以用它作为区分“收获”和“成长项目”的测试,因为“种子”和“后代”对应于“收获”和“成长项目”,我们也可以用它来检查“种子”和“后代”。

【讨论】:

  • 如果你不得不去这样的麻烦,这是一个明确的指标,该工具的使用方式错误。就像用锤子打螺丝一样。这是可能的,但要困难得多,结果很丑
  • 至于创可贴,我怀疑真正的创可贴是使用同一个类来踩不同有效载荷的最初想法。当这不起作用时,您发布了有关创可贴的问题,而不是实际问题。这称为the XY Problem
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-27
  • 2021-07-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多