Gson gson = new Gson();
gson.fromJson(jsonStr,YourClass.class);

very easy.

Answer from David Wang on Stack Overflow
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ json โ€บ convert string to jsonobject with gson
Convert String to JsonObject with Gson | Baeldung
May 5, 2025 - Learn a couple of methods for converting a JSON String into a JsonObject using the Gson library in Java.
Top answer
1 of 2
35
Gson gson = new Gson();
gson.fromJson(jsonStr,YourClass.class);

very easy.

2 of 2
25

The reason for the error is that your JSON at the top level is an array, not an object. That is covered by GSON throwing "Expected BEGIN_OBJECT but was BEGIN_ARRAY"?.

However, the solution there won't work for your JSON because you have an array of mixed types (an object and an array) rather than an array of a single type of object. For that you're going to have to write a custom deserializer (See The section of the Gson user's guide that covers this) or use Gson's JsonParser class directly and extract the data from the parse tree.

Edit from comments above:

If you're the one creating the JSON, it looks like what you want is an array of BoxSearch objects?

Based on your Java BoxSearch class, you'd need JSON structured like:

[
    {
        "lat1" : 39.737567,
        "lat2" : 32.7801399,
        "long1" : -104.98471790000002,
        "long2" : -96.80045109999998,
        "boxes" : [ 
                    {
                      "lat": {
                          "b": 38.88368709500021,
                          "d": 40.620468491667026
                      },
                      "long": {
                          "b": -105.75306170749764,
                          "d": -104.675854661387
                      }
                    }
                  ]
    }
]

However, the way you have Boxes class defined won't work for that. (Did you mean to have an array of them?). As-is it would need to look like:

class Boxes {
    Box lat;
    @SerializedName("long")
    Box lon;
}

class Box {
   String b;
   String d;
}

Now you have an array containing one type of object (BoxSearch) which you could deserialize with:

Type collectionType = new TypeToken<Collection<BoxSearch>>(){}.getType();
Collection<BoxSearch> boxSearchCollection = gson.fromJson(json, collectionType);

If you really don't need an array of these, get rid of the outer array and simply do:

gson.fromJson(json, BoxSearch.class);
๐ŸŒ
Java67
java67.com โ€บ 2017 โ€บ 05 โ€บ how-to-convert-java-object-to-json-using-Gson-example-tutorial.html
How to convert Java object to JSON String using Gson? Example Tutorial | Java67
For now, let's just focus on serializing a simple Java object to JSON. Next, we need to call the toJSon() function of Gson and pass the UserDetails object to this method. This will return the JSON String you are looking after.
๐ŸŒ
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(); // A Java object Staff obj = new Staff(); // Converts Java object to JSON string String json = gson.toJson(obj); // Converts Java object to File try (Writer writer = new FileWriter("staff.json")) { gson.toJson(staff, ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ convert-json-string-to-java-object-using-gson
Convert Json String to Java Object Using GSON - GeeksforGeeks
July 11, 2025 - GSON: It is an open-source Java library which is used to serialize and deserialize Java objects to JSON. ... In this article, a predefined JSON String is converted into Java Object using GSON.
๐ŸŒ
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 - 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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ convert-java-object-to-json-string-using-gson
Convert Java Object to Json String using GSON - GeeksforGeeks
July 11, 2025 - Create a Java class for converting the Organisation object into JSON. ... package GeeksforGeeks.Geeks; import com.google.gson.Gson; public class ObjectToJson { public static void main(String[] a) { /**Creating object of Organisation **/ Organisation ...
Find elsewhere
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-convert-java-object-to-json-using-gson-library
How to convert Java object to JSON using GSON library?
October 10, 2019 - Invoke the toJson() method by passing the above created POJO object. Retrieve and print the obtained JSON. import com.google.gson.Gson; class Student { private int id; private String name; private int age; private long phone; public int getId() { return id; } public void setId(int id) { this.id ...
๐ŸŒ
Javatpoint
javatpoint.com โ€บ convert-java-object-to-json-using-gson
Convert Java Object to Json using GSON - Javatpoint
November 19, 2013 - Convert Java Object to Json using GSON with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, examples etc.
๐ŸŒ
GitHub
github.com โ€บ google โ€บ gson
GitHub - google/gson: A Java serialization/deserialization library to convert Java Objects into JSON and back ยท GitHub
A Java serialization/deserialization library to convert Java Objects into JSON and back - google/gson
Starred by 24.2K users
Forked by 4.4K users
Languages ย  Java
๐ŸŒ
Java67
java67.com โ€บ 2017 โ€บ 05 โ€บ how-to-convert-json-string-to-java-object-gson-example.html
How to convert JSON String to Java Object using Gson? JSON Deserialization Example | Java67
Even though JSON standard defines that JSON properties should be wrapped in quotation marks or double quotes " ", you can use single quotes to avoid tons of \" Java escaping in your String. Thankfully, Gson accepts keys in both single quotes and double quotes like you can write either "name" or 'name', both are valid. Using single quotes or apostrophes will make your code more readable, as shown below: String json = "{ 'name':'John', 'email':'john.doe@gmail.com', 'age':29, 'phone':5168161922, 'city':'NewYork', 'hasCreditCard':false }"; You can see here we don't need to escape any String inside JSON, which makes it more readable.
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java โ€บ how to parse json using gson
How to parse JSON using Gson - Mkyong.com
May 2, 2024 - JSON containing objects as JSON array. { "data": [ {"id": 1000, "name": "ABC"}, {"id": 1001, "name": "BCD"}, {"id": 1002, "name": "CDE"} ] } We can define Java classes that map to the structure of the JSON. ... package com.mkyong.json.gson.model; public class DataItem { private int id; private String name; @Override public String toString() { return "DataItem{" + "id=" + id + ", name='" + name + '\'' + '}'; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
๐ŸŒ
Mysamplecode
mysamplecode.com โ€บ 2012 โ€บ 05 โ€บ java-convert-object-json-string.html
Programmers Sample Guide: Java convert object to JSON string example using Gson toJson() method
This sample Java program generates a JSON String from the Java Object and then saves it to a file. You can choose to send your JSON String data as HTTP response to some AJAX request. package com.as400samplecode; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import com.as400samplecode.util.OrderDetail; import com.as400samplecode.util.OrderHeader; import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class GenerateJSON { public static void main(String[] args) { OrderHeader orderHeader = new
๐ŸŒ
Medium
medium.com โ€บ @nishamishra1017 โ€บ how-to-convert-java-objects-to-json-in-java-using-gson-56b8d7ca4201
How to Convert Java Objects to JSON in Java Using GSON | by Nisha Kumari | Medium
January 5, 2026 - In this example, we create a custom serializer and deserializer for java.util.Date to format dates as dd/MM/yyyy. This class tells Gson how to convert a Date object into a JSON string with the format dd/MM/yyyy.
๐ŸŒ
Medium
medium.com โ€บ @prathameshk3707 โ€บ json-serialization-and-deserialization-with-gson-ba8420a4265d
JSON Serialization and Deserialization with Gson | by Prathamesh Kodgire | Medium
May 12, 2024 - Gson provides a straightforward API for converting Java objects to JSON and vice versa. Letโ€™s look at a simple example: import com.google.gson.Gson; public class Main { public static void main(String[] args) { Gson gson = new Gson(); // Serialize ...
๐ŸŒ
HowToDoInJava
howtodoinjava.com โ€บ home โ€บ gson โ€บ gson tutorial: read and write json with examples
Gson Tutorial: Read and Write JSON with Examples
August 27, 2023 - So, we are free to reuse the same object for multiple JSON serialization and deserialization operations. Gson gson = new Gson(); // POJO -> JSON String String json = gson.toJson(new User(1, "Lokesh")); // JSON String -> POJO User user = ...
๐ŸŒ
W3Docs
w3docs.com โ€บ java
Convert JsonObject to String
Here is an example of how to do ... JsonObject, such as {"key":"value"}. Alternatively, you can use the Gson library to convert the JsonObject to a JSON string....
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ convert-java-object-pojo-to-and-from-json-with-gson
Convert Java Object (POJO) To and From JSON with Gson
June 15, 2021 - In this tutorial, we'll take a look at examples on how to convert Java Object (POJO) to JSON and how to convert JSON to Java Object using Gson.