Carl
Carl

Reputation: 618

Java Parsing JSON with GSON

I am new to Java and JSON and I'm trying to parse the following JSON using GSON. However, I am having a problem in that I don't have any errors but the object is just empty.

{
    "pac": [
        {
            "customerName": "TEST"
        }
    ]
}

This class is what im trying to make an object of:

public class customer{

/** The customer name. */
private String customerName;

/**
 * Gets the customer name.
 *
 * @return the customer name
 */
public String getCustomerName() {
    return customerName;
}

/**
 * Sets the customer name.
 *
 * @param customerName the new customer name
 */
public void setCustomerName(String customerName) {
    this.customerName = customerName;
}

I'm using this to try and parse:

Gson gson = new Gson();
customer i = gson.fromJson(jsonFile, customer.class);

I would appriciate if you guys had any tips.

Upvotes: 1

Views: 115

Answers (1)

Hacketo
Hacketo

Reputation: 4997

Your JSON show that there is an Object that have a property pac.

This property pac is an Array of customer

So you could try with :

public class Customers {

    public List<customer> pac; // List from java.util

}

and then

Customers i = gson.fromJson(jsonFile, Customers.class);

Upvotes: 4

Related Questions