3

Here's an example of my JSON:

{
    "status": "ok",
    "rowCount": 60,
    "pageCount": 6,
    "value": [{
        "CustomerID": 1911,
        "CustomerTypeID": 3,
           ...
         }
       ]
}

My POJO:

@SerializedName("CustomerID")
public Integer CustomerID;

@SerializedName("CustomerTypeID")
public Integer CustomerTypeID;

I want to pull everything under value.

How do I do this using Google's GSON?

I've tried doing it as I would normally, but for, obvious reasons, it didn't work:

Type collectionType = new TypeToken<ArrayList<Customer>>() {}.getType();
return gson.fromJson(json, collectionType);
2
  • github.com/google/gson/blob/master/UserGuide.md Commented Apr 1, 2019 at 7:53
  • GsonBuilder gsonBuilder = new GsonBuilder(); gson = gsonBuilder.create(); ExampleClass resultObj = gson.fromJson(jsonObject.toString(), ExampleClass.class); List<Value> yourListOfCustomerValues = resultObj.getValue(); Commented Apr 1, 2019 at 12:35

3 Answers 3

2

You can not skip root JSON object. The simplest solution in this case is - create root POJO:

class Response {
    @SerializedName("value")
    private List<Customer> customers;

    // getters, setters
}

And you can use it as below:

return gson.fromJson(json, Response.class).getCustomers();
Sign up to request clarification or add additional context in comments.

2 Comments

Where did getCustomers() come from?
@IgorGanapolsky, you need to create a getter method for List<Customer> customers field.
1

You don't need to worry writing your own POJO.

just visit http://www.jsonschema2pojo.org/ and paste here your JSON data, it'll automatically return you converted classes as below

-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

@SerializedName("status")
@Expose
private String status;
@SerializedName("rowCount")
@Expose
private Integer rowCount;
@SerializedName("pageCount")
@Expose
private Integer pageCount;
@SerializedName("value")
@Expose
private List<Value> value = null;

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public Integer getRowCount() {
return rowCount;
}

public void setRowCount(Integer rowCount) {
this.rowCount = rowCount;
}

public Integer getPageCount() {
return pageCount;
}

public void setPageCount(Integer pageCount) {
this.pageCount = pageCount;
}

public List<Value> getValue() {
return value;
}

public void setValue(List<Value> value) {
this.value = value;
}

}

-----------------------------------com.example.Value.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Value {

@SerializedName("CustomerID")
@Expose
private Integer customerID;
@SerializedName("CustomerTypeID")
@Expose
private Integer customerTypeID;

public Integer getCustomerID() {
return customerID;
}

public void setCustomerID(Integer customerID) {
this.customerID = customerID;
}

public Integer getCustomerTypeID() {
return customerTypeID;
}

public void setCustomerTypeID(Integer customerTypeID) {
this.customerTypeID = customerTypeID;
}

}

The above two classes are auto generated by website.

Comments

0
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

 public class ExampleClass {

 @SerializedName("status")
 @Expose
 private String status;
 @SerializedName("rowCount")
 @Expose
 private int rowCount;
 @SerializedName("pageCount")
 @Expose
 private int pageCount;
 @SerializedName("value")
 @Expose
 private List<Value> value = null;

 public String getStatus() {
 return status;
  }

 public void setStatus(String status) {
this.status = status;
 }

 public int getRowCount() {
 return rowCount;
  }

  public void setRowCount(int rowCount) {
 this.rowCount = rowCount;
 }

 public int getPageCount() {
   return pageCount;
   }

 public void setPageCount(int pageCount) {
   this.pageCount = pageCount;
   }

  public List<Value> getValue() {
 return value;
 }

  public void setValue(List<Value> value) {
 this.value = value;
  }

 }

-----------------------------------Value.java-----------------------------------

 import com.google.gson.annotations.Expose;
 import com.google.gson.annotations.SerializedName;

 public class Value {

 @SerializedName("CustomerID")
 @Expose
 private int customerID;
 @SerializedName("CustomerTypeID")
 @Expose
 private int customerTypeID;

 public int getCustomerID() {
 return customerID;
}

 public void setCustomerID(int customerID) {
this.customerID = customerID;
}

public int getCustomerTypeID() {
return customerTypeID;
}

public void setCustomerTypeID(int customerTypeID) {
this.customerTypeID = customerTypeID;
 }

 }

/********* parsing with Gson ******/

    GsonBuilder gsonBuilder = new GsonBuilder(); 
    gson = gsonBuilder.create(); 
    ExampleClass resultObj = gson.fromJson(jsonObject.toString(), ExampleClass.class);
    List<Value> yourListOfCustomerValues = resultObj.getValue();

You can refer to this amazing post on mapping of arrays and lists of objects with Gson by Norman Peitek

Basics of Gson, model annotations and mapping of nested objects

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.