Retrofit2:将具有动态键的JSON转换为Map< String,Model>与模型也包含这些键 [英] Retrofit2: convert JSON with dynamic keys to a Map<String, Model> with Model also containing those keys

查看:148
本文介绍了Retrofit2:将具有动态键的JSON转换为Map< String,Model>与模型也包含这些键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Retrofit 2与Gson和RxJava结合使用.我的JSON数据如下所示:

I'm using Retrofit 2 in combination with Gson and RxJava. My JSON data looks something like this:

{
    "groups": {
         "1": {
              "name": "First group",
              "type": "some data",
              // ... more data
         },
         "2": {
              "name": "Second group",
              "type": "some data",
              // ... more data
         },
         "3": {
              "name": "Third group",
              "type": "some data",
              // ... more data
         }
         // 4, 5, 6, etc...
    },
    // ... more data
}

在上面的示例中,键" 1、2、3是整数,但它们也可以是唯一的字符串.我想将此JSON数据映射到这样的内容:

In the example above the "keys" 1, 2, 3 are integers but they can also be unique strings. I want to map this JSON data to something like this:

public class MyData {
    @SerializedName("groups")
    private Map<String, Group> mGroups;

    // ... more data
}

public class Group {
    // TODO: This should be "1", "2", "3", etc.
    @SerializedName(???)
    private String mId;

    // This should be "First group", "Second group", "Third group", etc.
    @SerializedName("name")
    private String mName;

    // This should be "some data"
    @SerializedName("type")
    private String mType;

    // ... more data
}

将动态键(1、2、3)放入Group对象的最佳方法是什么?我不是自己托管JSON数据,因此无法将其更改为另一种格式.

What's the best way to put the dynamic key (1, 2, 3) in the Group object as well? I'm not hosting the JSON data myself so it cannot be changed to another format.

推荐答案

步骤A-创建组类:

public class Group {
    String name;
    String type;
}

步骤B-创建群组类:

public class Groups {
    List<Group> userList;
}

步骤C-创建GSON反序列化器类

Step C - Create a GSON Deserializer class

public class MyDeserializer implements JsonDeserializer<Groups> {
    private final String groups_key = "groups";

    @Override
    public Groups deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        Groups groups = new Groups();
        JsonObject object = json.getAsJsonObject().getAsJsonObject(groups_key);
        Map<String, Group> retMap = new Gson().fromJson(object, new TypeToken<HashMap<String, Group>>() {}.getType());

        List<Group> list = new ArrayList<Group>(retMap.values());
        groups.userList = list;

        return groups;
    }
}

步骤D-创建Gson对象时注册反序列化器

Step D - Register your Deserializer when you create your Gson object

Gson gson = new GsonBuilder()
                        .registerTypeAdapter(Groups.class, new MyDeserializer()).create();

步骤E-通过转换JSON对象.吉森

Step E - Convert your JSON object via. Gson

Groups groups = gson.fromJson(jsonExmple, Groups.class);

注意:

  • 当JSON对象变大时,您可以扩展Group(组) 类并添加更多变量.请记住,您将需要在Deserializer类中反映所有内容. :)
  • 当变量的名称与JSON键的名称不同时,请使用@SerializedName批注
  • When your JSON object gets bigger you can expand your Group/Groups classes and add more variables. Keep in mind that you will need to reflect everything in your Deserializer class. :)
  • Use @SerializedName annotation when your variable got a different name from your JSON key

这篇关于Retrofit2:将具有动态键的JSON转换为Map&lt; String,Model&gt;与模型也包含这些键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆