How to convert ArrayList of custom class to JsonArray in Java?

前端 未结 7 2098
一个人的身影
一个人的身影 2021-02-03 22:18

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 A

7条回答
  •  情歌与酒
    2021-02-03 22:43

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

提交回复
热议问题