【问题标题】:Parse a nested dynamic module using Gson使用 Gson 解析嵌套的动态模块
【发布时间】:2021-08-06 01:20:22
【问题描述】:

我有一个或多或少像这样的 JSON:

{
  "modules": {
    "someExistingModule": {
      "name": "pug",
      ...
    },

    "randomExistingModule": {
      "type": "cat",
      ...
    },

    "myNewModule": {   // <----- I care about this module. Note that this is NOT an array
      "modules": {
        "img1": {
          "type": "image",
          "url": "https://some/random/image,
          "altText": "Some image description
        },
        "img2": {
          "type": "image",
          "url": "https://some/random/image,
          "altText": "Some image description
        },
        "img3": {
          "type": "image",
          "url": "https://some/random/image,
          "altText": "Some image description
        },
        "txt1": {           // <------ Updated JSON
          "type": "text",
          "content": "Hello world 1"
        },
        "txt2": {
          "type": "text",
          "content": "Hello world 2"
        },
        ...
      }
    }

myModule 内部可以有NimgN 对象和txtN。我需要动态解析它。

我当前的 Response 类如下所示:

public class MyModuleResponse extends SomeResponseClass
{
  @Override
    public void parse(InputStream data)
    {
        T responseBody = readJsonStream(data, MyModuleResponseBody.class());
        MyModuleDataParser.parse(responseBody);
    }

MyModuleDataParser.java

...
public static MyModuleDataParser parse(@Nullable MyModuleResponseBody body)
{
  parseSomeExistingModule();

  parseRandomExistingModule();

  parseMyNewModule(); // <--- This is the new module I'm trying to parse. Currently, this method is empty.

}

MyModuleResponseBody.java

public class MyModuleResponseBody
{
  public Modules modules;

  public static class Modules
  {
    SomeExistingModule someExistingModule;
    RandomExistingModule randomExistingModule;
    MyNewModule myNewModule; // <----- My new module
  }

  public static class SomeExistingModule
  {
    String name;
    ...
  }

  public static class RandomExistingModule
  {
    String type;
    ...
  }

  public static class MyNewModule
    {
        public ??? modules;   // <--- Trying to define the Type here. Something like List<MyImageModule>. But, it won't work
    }

MyImageModule.java

public class MyImageModule extends Module // <---- Update: This class now extends a generic Module class
{
  private String url;
  private String altText;
}

MyTextModule.java

public class MyTextModule extends Module    // New class
{
  private String content;
}

Module.java

public class Module  // <----- New parent class
{
  protected String type;
}

如何从myNewModule 创建MyImageModule 列表?我相信我需要使用来自 Gson 库的某种 TypeAdapter。但是,我不熟悉如何在现有响应中执行此操作。

【问题讨论】:

    标签: java gson


    【解决方案1】:

    使用Map&lt;String, MyImageModule&gt;,其实就是一个hashmap来解决json中non-list modules对象的问题。

    public static class MyNewModule {
        public Map<String, MyImageModule> modules; // initialize as a hashmap
    }
    

    【讨论】:

    • 行得通!但是,让我们内部的modules 可以有MyImageModuleMyTextModule。这两个模块都从Module 扩展而来。我将如何定义这张地图?
    • 您可以将它们映射到Map&lt;String, Object&gt;,然后在其他操作期间将对象转换为相关的对象,例如通过keys,您知道图像的键是什么,@ 内的文本是什么987654331@json.
    • 我应该将它们映射到Map&lt;String,Object&gt; 还是可以使用Map&lt;String,Module&gt;?我尝试了后者,似乎只拿着钥匙。值设置为null。我用新的类和 JSON 更新了我的问题描述
    • 不,您应该将它们映射到 Map&lt;String,Object&gt; 并在其上执行 get(key) 并将其转换为所需的类
    猜你喜欢
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多