1

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.

4
  • 1
    Your JSON file is not in proper format check json.org/example . Commented Jul 22, 2015 at 13:12
  • 1
    sorry that was an error when i posted the correct json is now in the question Commented Jul 22, 2015 at 13:14
  • @JishnuPrathap it is correct JSON, that it isn't formatted correctly doesn't matter, or at least shouldn't matter for any parser Commented Jul 22, 2015 at 13:22
  • 1
    @engineercoding format matter.. (not talking about spaces and indent) and OP has updated the JSON (if you saw his comment) Commented Jul 22, 2015 at 13:24

1 Answer 1

4

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);
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.