如何解析密钥名称为整数的json

时间:2018-02-19 04:45:05

标签: java json gson

我需要像这样解析json:

{
"response": {
    "70": [326707130, 320565529, 218874712, 195318591, 272944693, 136909660, 384774802, 9486342, 5663588, 245478751, 437283231],
    "75": [205268343, 307729010, 272944693, 384774802, 312530843, 220948861, 270477243]
}
}

我正在使用GSON,我不知道该怎么做,因为我不能用Java命名这个变量:

public int[] 70;

1 个答案:

答案 0 :(得分:4)

您可以使用@SerializedNameMap

最简单的是Map

class Root {
    Map<String, List<Integer>> response;
}

键也可以是整数:

class Root {
    Map<Integer, List<Integer>> response;
}

但如果您更喜欢定义的字段,请使用@SerializedName

class Root {
    Response response;
}

class Response {
    @SerializedName("70")
    List<Integer> listA;

    @SerializedName("75")
    List<Integer> listB;
}

在所有情况下,您都使用以下方式阅读JSON:

Root root = gson.fromJson(in, Root.class);
相关问题