简体   繁体   English

改造 - Json 响应可以根据类型包含不同的字段

[英]Retrofit - Json response can contain different fields depending on type

I'm working on an API wrapper using retrofit but I'm having issues creating the returned object classes since some items from the same endpoint can contain different type of fields.我正在使用改造处理 API 包装器,但我在创建返回的对象类时遇到问题,因为来自同一端点的某些项目可能包含不同类型的字段。

For example, the returning JSON can look like this for a basic item:例如,对于基本项,返回的 JSON 可能如下所示:

{
  "name": "Box of Honed Splint Armor",
  "description": "Double-click to unpack a full set of level 50 armor.",
  "type": "Container",
  "level": 0,
  "rarity": "Masterwork",
  "vendor_value": 86,
  "game_types": [
    "Wvw",
    "Dungeon",
    "Pve"
  ],
  "flags": [],
  "restrictions": [],
  "id": 9000,
  "chat_link": "[&AgEoIwAA]",
  "icon": "https://render.guildwars2.com/file/72D04673660ECB7FD904680D487030A41106F952/63218.png",
  "details": {
    "type": "Default"
  }
}

Or like this for a more detailed item:或者像这样获取更详细的项目:

{
  "name": "Strong Soft Wood Longbow of Fire",
  "description": "",
  "type": "Weapon",
  "level": 44,
  "rarity": "Masterwork",
  "vendor_value": 120,
  "default_skin": "3942",
  "game_types": [ "Activity", "Dungeon", "Pve", "Wvw" ],
  "flags": [ "SoulBindOnUse" ],
  "restrictions": [],
  "id": 28445,
  "chat_link":"[&AgEdbwAA]",
  "icon": "https://render.guildwars2.com/file/C6110F52DF5AFE0F00A56F9E143E9732176DDDE9/65015.png",
  "details": {
    "type": "LongBow",
    "damage_type": "Physical",
    "min_power": 385,
    "max_power": 452,
    "defense": 0,
    "infusion_slots": [],
    "infix_upgrade": {
      "attributes": [
        { "attribute": "Power", "modifier": 62 },
        { "attribute": "Precision", "modifier": 44 }
      ]
    },
    "suffix_item_id": 24547,
    "secondary_suffix_item_id": ""
  }
}

The details field can differ very much between different item types, and I'd prefer to have an easy way to access the fields corresponding to each type without the wrapper consumer has to type cast to a type-specific subclass of an empty base class like other wrappers require, like one of the best wrappers do.不同项目类型之间的详细信息字段可能会有很大差异,我更喜欢有一种简单的方法来访问与每种类型对应的字段,而无需包装器使用者必须将类型转换为空基类的特定于类型的子类,例如其他包装器需要,就像最好的包装器之一一样。

Is there some way to hide fields depending on the type of the parent or something like that so I can include all possible fields in the item class?有没有办法根据父项的类型或类似的东西隐藏字段,以便我可以在项目类中包含所有可能的字段?

Any suggestions?有什么建议?

As per https://www.baeldung.com/retrofit根据https://www.baeldung.com/retrofit

Retrofit works by modeling over a base URL and by making interfaces return the entities from the REST endpoint. Retrofit 的工作原理是对基本 URL 进行建模,并使接口从 REST 端点返回实体。

For simplicity purposes we're going to take a small part of the JSON by modeling our User class that is going to take the values when we have received them:为简单起见,我们将通过建模我们的 User 类来获取 JSON 的一小部分,该类将在我们收到值时获取值:

public class User {
    private String login;
    private long id;
    private String url;
    // ...

    // standard getters an setters

}

We can see that we're only taking a subset of properties for this example.我们可以看到,对于这个例子,我们只采用了属性的一个子集。 Retrofit won't complain about missing properties – since it only maps what we need, it won't even complain if we were to add properties that are not in the JSON. Retrofit 不会抱怨缺少属性——因为它只映射我们需要的东西,如果我们要添加不在 JSON 中的属性,它甚至不会抱怨。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM