Learn to use Google GSON library to deserialize or parse a JSON array to a Java array or List object. It’s worth mentioning that JSON has only array datatype. Java has both – arrays and lists. 1. Parsing JSON Array as Root To parse JSON, with array as …
Learn to use Google GSON library to deserialize or parse a JSON array to a Java array or List object. It’s worth mentioning that JSON has only array datatype. Java has both – arrays and lists.
1. Parsing JSON Array as Root
To parse JSON, with array as root, we can use the following method syntax. Here ArrayItem is the class type of data elements in the array.
User [id=1, name=Alex]
User [id=2, name=Brian]
User [id=3, name=Charles]
2. Parsing JSON Array as Member
Gson parses JSON arrays as members without difficulty if they are non-root objects. We can use the fromJson() method in the usual manner and it will parse the JSON array correctly to the required java array or list.
2.1. Member JSON Array to Java Array
Java program to deserialize JSON array as member object – to Java array of objects as member field.
publicclassDepartment{privatelong id;privateString name;privateUser[] users;//Getters and Setters}
A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies.
An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Comments