Home » Android » android – Parse JSON object with gson

android – Parse JSON object with gson

Posted by: admin June 15, 2020 Leave a comment


I’m trying to parse JSON like:

{"response":[123123, 1231231, 123124, 124124, 111111, 12314]}

With GSON, making

Gson gson = new GsonBuilder().create();
int[] friends = new Gson().fromJson(answer, int[].class);

But get Error Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2

How to parse this numbers in array?

How to&Answers:

You’re going to want to create a model class first that GSON can bind your json to:

public class ResponseModel {

    private List<Integer> response = new ArrayList<Integer>();

    public List<Integer> getResponse() {
        return response;

    public String toString() {
        return "ResponseModel [response=" + response + "]";

Then you can call

Gson gson = new Gson();
ResponseModel responseModel = gson.fromJson("{\"response\":[123123, 1231231, 123124, 124124, 111111, 12314]}",
List <Integer> responses = responseModel.getResponse();
// ... do something with the int list


The other option you have outside of using a wrapper class is simply to get the array from the parse tree.

Use the JsonParser to create the tree, get your array from it, then convert to int[] using Gson:

public class App 
    public static void main( String[] args ) throws IOException
       String json = "{\"response\":[1,2,3,4,5]}";

       JsonObject jo = new JsonParser().parse(json).getAsJsonObject();
       JsonArray jsonArray = jo.getAsJsonArray("response");

       int[] myArray = new Gson().fromJson(jsonArray, int[].class);



Note that you could also just work with the JsonArray directly and not convert it to an int[] depending on your use case.



Try this method

String json1 = "[{\"contactName\":\"3\",\"contactNumber\":\"3\"},{\"contactName\":\"4\",\"contactNumber\":\"4\"}]";

JsonElement json = new JsonParser().parse(json1);    
JsonArray array= json.getAsJsonArray();    
Iterator iterator = array.iterator();    
List<ContactDetail> details = new ArrayList<ContactDetail>();

    JsonElement json2 = (JsonElement)iterator.next();
    Gson gson = new Gson();
    ContactDetail contact = gson.fromJson(json2, ContactDetail.class);
    //can set some values in contact, if required 

I got another form here


BEGIN_ARRAY but was BEGIN_OBJECT means in the beginning it’s Object not the array. If you will see in the Json, response property is the type of object and that object further contains the array. You can Parse it with different ways Using creating your own custom model class or without creating custom model class. If you don’t wan’t to create your Response Model class then simply parse it

final Type typeOf = new TypeToken<Map<String, List<Integer>>>() {}.getType();
final Map<String, List<Integer>> map = new Gson().fromJson(Your_JSON_String, typeOf);

// finally get list of ints form map.
final List<Integer> listOfIntegers =map.get("response");


Json value has an object as response which holds an integer array. You have to define its class correctly first:

class ResponseHolder {
    private Integer[] response;


class ResponseHolder {
    private int[] response;


class ResponseHolder {
    private String[] response;

Then use that class while parsing your json value to a java instance:

ResponseHolder responseHolder = gson.fromJson(answer, ResponseHolder.class);


A Kotlin soulution

val myResponse = Gson().fromJson(responseStr, MyResponse::class.java)


Or use org.json included in Android:

JSONObject jsonData = new JSONObject(answer);
JSONArray jsonItems = jsonData.getJSONArray("response");