Convert JSON to Gson with type as ArrayList


If you have a GSON String which is an object collection and you want to convert it into an ArrayLis<> using the Gson library, make use of the reflection TypeToken

ArrayList<Student> stdList = new ArrayList<>();
stdList = gson.fromJson(jsonList,  new TypeToken<ArrayList<Student>>(){}.getType());
System.out.println(stdList(0).getStudentName());
System.out.println(stdList(1).getStudentName());
Example:
import com.google.gson.Gson;
    import com.google.gson.reflect.TypeToken;

    import java.util.ArrayList;

    public class JsonToGsonExample {

        public static void main(String[] args) {

            String jsonString = "{\n" +
                    "  \"studentId\": 1,\n" +
                    "  \"studentName\": \"Mike\",\n" +
                    "  \"studentAddress\": \"Paris\",\n" +
                    "  \"studentAge\": 19,\n" +
                    "  \"studentMarks\": {\n" +
                    "    \"English\": 87,\n" +
                    "    \"Chemistry\": 72,\n" +
                    "    \"Math\": 75,\n" +
                    "    \"Physics\": 67\n" +
                    "  }\n" +
                    "}";

            String jsonStringList ="[\n" +
                    "  {\n" +
                    "    \"studentId\": 1,\n" +
                    "    \"studentName\": \"Mike\",\n" +
                    "    \"studentAddress\": \"Paris\",\n" +
                    "    \"studentAge\": 19,\n" +
                    "    \"studentMarks\": {\n" +
                    "      \"English\": 87,\n" +
                    "      \"Chemistry\": 72,\n" +
                    "      \"Math\": 75,\n" +
                    "      \"Physics\": 67\n" +
                    "    }\n" +
                    "  },\n" +
                    "  {\n" +
                    "    \"studentId\": 2,\n" +
                    "    \"studentName\": \"Alex\",\n" +
                    "    \"studentAddress\": \"London\",\n" +
                    "    \"studentAge\": 18,\n" +
                    "    \"studentMarks\": {\n" +
                    "      \"English\": 71,\n" +
                    "      \"Chemistry\": 98,\n" +
                    "      \"Math\": 89,\n" +
                    "      \"Physics\": 76\n" +
                    "    }\n" +
                    "  }\n" +
                    "]";

            Gson gson = new Gson();

            //Example with just one Json Object
            Student student = gson.fromJson(jsonString,Student.class);
            System.out.println(student.getStudentName());
            System.out.println(student.getStudentAddress());

            //Example with a Student List
            ArrayList<Student> studentsList = new ArrayList<>();
            studentsList = gson.fromJson(jsonStringList,  new TypeToken<ArrayList<Student>>(){}.getType());
            System.out.println(studentsList.get(0).getStudentName());
            System.out.println(studentsList.get(1).getStudentName());
        }
    }
Advertisements
Try Out Code2care Dev Tools:

Advertisements

Advertisements
Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and software developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include learnings, tutorials, trouble-shooting steps, video tutorials, code snippets, how-to, blogs, articles, etc.

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.