【问题标题】:Gson same attributes but differents objectsGson 属性相同但对象不同
【发布时间】:2018-10-24 19:13:35
【问题描述】:

我正在使用 Gson 将 json 解析为我的对象:

@SerializedName("MyAttribute")
    private List<MyObject> myObject;

但在一次调用中,同一属性的答案可能会有所不同。 一方面:

"MyAttribute":[  
      "stringInformation",
      "stringInformation2"
   ]

这是一个字符串列表, 另一方面,它是一个对象列表:

"MyAttribute":[  
          {  
            "id":"info",
            "info":"info",
            "info2":"info
          },
          {  
            "id":"info",
            "info":"info",
            "info2":"info
          }
       ]

为了映射对象没关系,我的 Gson 设置为将其解析到我的对象列表中。但是,如果它只是我的 json 文件中的 Strgin 列表,我想创建这些对象的列表。 你知道如何处理它吗?谢谢

【问题讨论】:

  • 我认为您可以轻松地应用我对此this question 的答案,但这当然只是一种选择。

标签: java android parsing gson


【解决方案1】:

您必须为响应创建 2 个不同的对象:

@SerializedName("MyAttribute")
private List<Strings> myStrings;


@SerializedName("MyAttribute")
private List<MyObject> myObject;

【讨论】:

    猜你喜欢
    • 2018-11-03
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 2016-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多