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

前端 未结 7 2097
一个人的身影
一个人的身影 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:54

    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);
    
    0 讨论(0)
提交回复
热议问题