The JSON is invalid. A collection is not to be represented by {}. It stands for an object. A collection/array is to be represented by [] with commaseparated objects.

Here's how the JSON should look like:

{
    users:[{
        name: "name1",
        email: "email1",
        friends:[{
            name: "name2",
            email: "email2",
            friends:[{
                name: "name3",
                email: "email3"
            },
            {
                name: "name4",
                email: "email4"
            }]
        }]
    }]
}

(note that I added one more friend to the deepest nested friend, so that you understand how to specify multiple objects in a collection)

Given this JSON, your wrapper class should look like this:

public class Data {
    private List<User> users;
    // +getters/setters
}

public class User {
    private String name;
    private String email;
    private List<User> friends;
    // +getters/setters
}

and then to convert it, use

Data data = gson.fromJson(this.json, Data.class);

and to get the users, use

List<User> users = data.getUsers();
Answer from BalusC on Stack Overflow
🌐
GitHub
google.github.io › gson › UserGuide.html
Gson User Guide | gson
Register a type adapter for MyCollectionMemberType and use fromJson() with Collection<MyCollectionMemberType>. This approach is practical only if the array appears as a top-level element or if you can change the field type holding the collection to be of type Collection<MyCollectionMemberType>. ...
🌐
Mkyong
mkyong.com › home › java › convert java objects to from json using gson
Convert Java objects to from JSON using Gson - Mkyong.com
May 6, 2024 - Gson gson = new Gson(); // Converts JSON file to Java object try (Reader reader = new FileReader("staff.json")) { // Convert JSON File to Java Object Staff staff = gson.fromJson(reader, Staff.class); } catch (IOException e) { throw new ...
🌐
ZetCode
zetcode.com › java › gson
Java Gson - JSON serialization and deserialization in Java with Gson
October 10, 2024 - User[] users = gson.fromJson(reader, User[].class); The second parameter of fromJson is an array class. The following example reads JSON data from a web page. We get JSON data from http://time.jsontest.com. { "date": "10-09-2024", "milliseconds_since_epoch": 1728466050756, "time": "09:27:30 ...

The JSON is invalid. A collection is not to be represented by {}. It stands for an object. A collection/array is to be represented by [] with commaseparated objects.

Here's how the JSON should look like:

{
    users:[{
        name: "name1",
        email: "email1",
        friends:[{
            name: "name2",
            email: "email2",
            friends:[{
                name: "name3",
                email: "email3"
            },
            {
                name: "name4",
                email: "email4"
            }]
        }]
    }]
}

(note that I added one more friend to the deepest nested friend, so that you understand how to specify multiple objects in a collection)

Given this JSON, your wrapper class should look like this:

public class Data {
    private List<User> users;
    // +getters/setters
}

public class User {
    private String name;
    private String email;
    private List<User> friends;
    // +getters/setters
}

and then to convert it, use

Data data = gson.fromJson(this.json, Data.class);

and to get the users, use

List<User> users = data.getUsers();
Answer from BalusC on Stack Overflow
🌐
Tabnine
tabnine.com › home page › code › java › com.google.gson.gson
com.google.gson.Gson.fromJson java code examples | Tabnine
Map<String, String> myMap = new ... = new TypeToken<Map<String, String>>() { }.getType(); Map<String, String> newMap = gson.fromJson(json, typeOfHashMap); // This type must match TypeToken System.out.println(newMap.ge...
🌐
GitHub
github.com › google › gson
GitHub - google/gson: A Java serialization/deserialization library to convert Java Objects into JSON and back · GitHub
This avoids Gson's runtime crashes when optimizations are applied (usually due to the fields missing or being obfuscated), and results in faster performance on Android devices. The Moshi APIs may be more familiar to users who already know Gson. If you still want to use Gson and attempt to avoid these crashes, you can see how to do so here. Provide simple toJson() and fromJson() methods to convert Java objects to JSON and vice-versa
Starred by 24.3K users
Forked by 4.4K users
Languages   Java
🌐
HowToDoInJava
howtodoinjava.com › home › gson › gson tutorial: read and write json with examples
Gson Tutorial: Read and Write JSON with Examples
August 27, 2023 - String json = ...; Gson gson = new Gson(); User user = gson.fromJson(json, User.class); In most cases, Gson library is smart enough to create instances even if any class does not provide a default no-args constructor.
Find elsewhere
🌐
Crunchify
crunchify.com › json tutorials › how to use gson -> fromjson() to convert the specified json into an object of the specified class
How to use Gson -> fromJson() to convert the specified JSON into an Object of the Specified Class • Crunchify
February 16, 2023 - Basically – this same tutorial will help you if you want to convert gson fromjson to map, gson fromjson to list, json fromjson to array, json string to arraylist in java.
🌐
TutorialsPoint
tutorialspoint.com › gson › gson_quick_guide.htm
GSON - Quick Guide
Student student = new Student(); student.setRollNo(1); Student.Name name = new Student.Name(); name.firstName = "Mahesh"; name.lastName = "Kumar"; student.setName(name); //serialize static inner class object String nameString = gson.toJson(name); System.out.println(nameString); //deserialize static inner class object name = gson.fromJson(nameString,Student.Name.class); System.out.println(name.getClass()); Let's see an example of serialization/de-serialization of class with a static inner class in action.
🌐
TutorialsPoint
tutorialspoint.com › differences-between-fromjson-and-tojson-methods-of-gson-in-java
Differences between fromJson() and toJson() methods of Gson in Java?
May 13, 2025 - Here is an example of how to use ... static void main(String[] args) { Gson gson = new Gson(); String json = "{"name":"Aish", "age":22, "city":"Mumbai"}"; Person person = gson.fromJson(json, Person.class); System.out.println(person); } }...
🌐
Medium
medium.com › @prathameshk3707 › json-serialization-and-deserialization-with-gson-ba8420a4265d
JSON Serialization and Deserialization with Gson | by Prathamesh Kodgire | Medium
May 12, 2024 - Let’s look at a simple example: ... System.out.println("Serialized JSON: " + json); // Deserialize JSON to Java object MyClass newObj = gson.fromJson(json, MyClass.class); System.out.println("Deserialized Object: " + newObj); ...
🌐
Jenkov
jenkov.com › tutorials › java-json › gson.html
GSON - Java JSON Tutorial
February 18, 2016 - Using a GsonBuilder allows you to set configuration options on the GsonBuilder before creating the Gson object. You will see examples of this later in this GSON tutorial. GSON can pase JSON into Java objects using the fromJson() method of the Gson object.
🌐
Mkyong
mkyong.com › home › java › how to parse json using gson
How to parse JSON using Gson - Mkyong.com
May 2, 2024 - GsonParseJsonStringExample.java ... = "{\"name\": \"mkyong\", \"age\": 20}"; Gson gson = new Gson(); Person person = gson.fromJson(json, Person.class); System.out.println(person); } } JSON Array....
🌐
Adobe Developer
developer.adobe.com › experience-manager › reference-materials › 6-5 › javadoc › com › google › gson › Gson.html
Gson (The Adobe AEM Quickstart and Web Application.)
Here is an example of how Gson is used for a simple Class: Gson gson = new Gson(); // Or use new GsonBuilder().create(); MyType target = new MyType(); String json = gson.toJson(target); // serializes target to Json MyType target2 = gson.fromJson(json, MyType.class); // deserializes json into ...
🌐
DZone
dzone.com › coding › frameworks › json handling with gson in java with oop essence
JSON Handling With GSON in Java With OOP Essence
January 4, 2024 - This can be useful if you have previously converted an object into a JSON format and now need to retrieve the original object. The process involves using the GSON library to deserialize the JSON string and convert it into an object. This can be done using the fromJson() method, which takes in the JSON string and the object's class to be created.
🌐
Javadoc.io
javadoc.io › doc › com.google.code.gson › gson › 2.8.5 › com › google › gson › Gson.html
Gson - gson 2.8.5 javadoc
Bookmarks · Latest version of com.google.code.gson:gson · https://javadoc.io/doc/com.google.code.gson/gson · Current version 2.8.5 · https://javadoc.io/doc/com.google.code.gson/gson/2.8.5 · package-list path (used for javadoc generation -link option) · https://javadoc.io/doc/com.goog...
🌐
Baeldung
baeldung.com › home › json › gson deserialization cookbook
Gson Deserialization Cookbook | Baeldung
June 24, 2022 - @Test public void whenDeserializingToSimpleObject_thenCorrect() { String json = "{"intValue":1,"stringValue":"one"}"; Foo targetObject = new Gson().fromJson(json, Foo.class); assertEquals(targetObject.intValue, 1); assertEquals(targetObject.stringValue, "one"); } Explore the options available to exclude fields from serialization in Gson.
🌐
Medium
medium.com › @alexandre.therrien3 › personalized-serializer-and-deserializer-using-java-gson-library-c079de3974d4
Building a Personalized Serializer and Deserializer using Java Gson Library | by Alex Therrien | Medium
June 2, 2020 - GsonBuilder builder = new ... BufferedReader( new FileReader(canonicalPathToFile)); SomeObject someObject = gson.fromJson(bufferedReader, SomeObject.class); Remember that the SomeObject class is only used as an ...
🌐
GitHub
github.com › google › gson › blob › main › UserGuide.md
gson/UserGuide.md at main · google/gson
Register a type adapter for MyCollectionMemberType and use fromJson() with Collection<MyCollectionMemberType>. This approach is practical only if the array appears as a top-level element or if you can change the field type holding the collection to be of type Collection<MyCollectionMemberType>. Gson has built-in serializers and deserializers for commonly used classes whose default representation may be inappropriate, for instance
Author   google