How to Create JSONArray for a List

后端 未结 7 2049
南旧
南旧 2020-12-30 00:47

I have a class called

class Student {
   String name;
   String age;
}

I have a method that returns List object like

publi         


        
7条回答
  •  星月不相逢
    2020-12-30 01:36

    Using Gson Library it will be very simple.

    From JSON String to ArrayList of Object as:

    Type listType = 
         new TypeToken>(){}.getType();
    ArrayList yourClassList = new Gson().fromJson(jsonArray, listType);
    

    And to Json from Array List of Object as:

    ArrayList sampleList = new ArrayList();
    String json = new Gson().toJson(sampleList);
    

    The Gson Library is more simple to use than JSONObject and JSONArray implementation.

提交回复
热议问题