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());
        }
    }


















Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap