1

I need to parse json like this:

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

Im using GSON, and i dont know what to do, because i can`t name the variable in Java like this:

public int[] 70;
1
  • 1
    Note: the keys in your example aren't numbers. They're strings that happens to contain only digits. Commented Feb 19, 2018 at 5:34

1 Answer 1

4

You can use @SerializedName or a Map.

Easiest is a Map:

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

The keys can be integers too:

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

But if you prefer defined fields, use @SerializedName:

class Root {
    Response response;
}

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

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

In all cases you read the JSON using:

Root root = gson.fromJson(in, Root.class);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.