32

I am trying to convert ArrayList of custom class to JsonArray. Below is my code. It executes fine but some JsonArray elements come as zeros even though they are numbers in the ArrayList. I have tried to print them out. Like customerOne age in the ArrayList is 35 but it is 0 in the JsonArray. What could be wrong?

    ArrayList<Customer> customerList = CustomerDB.selectAll();
    Gson gson = new Gson();

    JsonElement element = 
     gson.toJsonTree(customerList , new TypeToken<List<Customer>>() {}.getType());

    JsonArray jsonArray = element.getAsJsonArray();

6 Answers 6

45

Below code should work for your case.

List<Customer> customerList = CustomerDB.selectAll();

Gson gson = new Gson();
JsonElement element = gson.toJsonTree(customerList, new TypeToken<List<Customer>>() {}.getType());

if (! element.isJsonArray() ) {
// fail appropriately
    throw new SomeException();
}

JsonArray jsonArray = element.getAsJsonArray();

Heck, use List interface to collect values before converting it JSON Tree.

Sign up to request clarification or add additional context in comments.

Comments

14

As an additional answer, it can also be made shorter.

List<Customer> customerList = CustomerDB.selectAll();

JsonArray result = (JsonArray) new Gson().toJsonTree(customerList,
            new TypeToken<List<Customer>>() {
            }.getType());

Comments

6

Don't know how well this solution performs compared to the other answers but this is another way of doing it, which is quite clean and should be enough for most cases.

ArrayList<Customer> customerList = CustomerDB.selectAll();
Gson gson = new Gson();
String data = gson.toJson(customerList);
JsonArray jsonArray = new JsonParser().parse(data).getAsJsonArray();

Would love to hear from someone else though if, and then how, inefficient this actually is.

1 Comment

At first step you are serializing the list to string and then you deserialize the string. So it is really inefficient.
3

Consider a list of Objects of type Model.class

ArrayList<Model> listOfObjects = new ArrayList<Model>();

List to JSON

String jsonText =  new Gson().toJson(listOfObjects);

JSON to LIST

 Type listType = new TypeToken<List<Model>>() {}.getType();

 List<Model> myModelList = new Gson().fromJson(jsonText , listType);

Comments

3

For Anyone who is doing it in Kotlin, you can get it this way,

val gsonHandler = Gson()
val element: JsonElement = gsonHandler.toJsonTree(yourListOfObjects, object : TypeToken<List<YourModelClass>>() {}.type)

Comments

1

List<> is a normal java object, and can be successfully transformed using standard gson object api. List in gson looks like this:

"libraries": [
    {
      //Containing object 
    },
    {
      //Containing object 
    }
   ],
...

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.