【问题标题】:Retrofit + GSON deserializer改造 + GSON 解串器
【发布时间】:2015-06-11 07:10:30
【问题描述】:

我有一个像这样的对象:

"choice": {
    "000": {
             "id": "001",
             "label": "test",
             "status": "0"
     },
    "001": {
             "id": "001",
             "label": "test",
             "status": "0"
     },
    "002": {
             "id": "001",
             "label": "test",
             "status": "0"
     },
    "003": {
             "id": "001",
             "label": "test",
             "status": "0"
     },
    "004": {
             "id": "001",
             "label": "test",
             "status": "0"
     }
    },

如何使用 Gson+Retrofit 解析该对象?或者生成一个POJO?有一种简单的方法吗?

非常感谢!

【问题讨论】:

    标签: android dictionary gson retrofit pojo


    【解决方案1】:

    您在选择 json 对象中所拥有的主要思想是 Map:

    public class RootObject{
        Map <String,ChoiceEntry> choice;
    }
    
    public class ChoiceEntry{
        String id;
        String label;
        int status;
    }
    

    【讨论】:

    • 您还可以编辑您的问题并提供“map”“序列化”gson 关键字,以使您的问题和我的答案更容易被其他有同样问题的人所接受。
    • [["test","test"],["test","test"],["test","test"],["test","test"],[ "test","test"],["test","test"],["test","test"]] 我有一个带有自定义对象列表的 POJO(带有 2 个字符串)。但它不起作用。
    • @Chronos 因为 [] 表示它是数组(列表)。所以现在你有了数组数组。你应该有 [{...},{...},{...},{...},{...}‌​,{...},{...}]。在这种情况下,它是对象列表。在 {"key1":"string1", "key2":"string2" } 内。它将是对象列表。
    【解决方案2】:

    您可以通过将该 JSON 代码粘贴到此链接中来创建 POJO:http://pojo.sodhanalibrary.com/

    您发布的 sn-p 格式不正确。我相信您将拥有一个多类 POJO,这对于某些用途(例如列表视图)来说很难使用。

    让我知道进展如何。 Retrofit 真的很好用,但是弄起来非常烦人!

    【讨论】:

    【解决方案3】:

    有一个addon 用于 intellij/android studio,用于轻松生成 POJO。

    免责声明:我与该项目无关。

    【讨论】:

      猜你喜欢
      • 2018-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-25
      • 2017-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多