Java - How to parse json file using Gson

gson read json file to jsonobject
gson read json array from file
gson parse json array
java read json file
java parse json string
gson read json file from resources
gson(); jsonobject example
convert json string to map in java using gson

I have a simple json file like this

{ 
  "user":"giovanni"
}

This is the class I wrote in java:

package maven.project;

import java.io.*;
import com.google.gson.*;

public class Prova {
    public static void main(String[] args)throws JsonSyntaxException, 
     JsonIOException, FileNotFoundException{

        String path = "/Users/matte/Desktop/project/src/main/java/maven/project/1.json";
        BufferedReader bufferedReader = new BufferedReader(new FileReader(path));
        Gson gson = new Gson();
        JsonObject js = gson.fromJson(bufferedReader, JsonObject.class);
        String user =  js.get("user").getAsString();
        System.out.println("user: " + user);
    }
}

but running it I find this error

Exception in thread "main" com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 3 column 26
        at com.google.gson.Gson.fromJson(Gson.java:809)
        at com.google.gson.Gson.fromJson(Gson.java:734)
        at Prova.main(Prova.java:11)
Caused by: com.google.gson.stream.MalformedJsonException: Unterminated object at line 3 column 26
        at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1310)

..

How can I solve?

Did you copy and paste the json file into the IDE ? Any chance it is carrying hidden characters or some other corruption ? If so maybe create a new json file and try that.

How to Read JSON from a File using Gson (Example), Gson is a popular JSON processing library developed and maintained by Google for reading, writing, and parsing JSON data in Java. 4. JSON to Java Objects. 4.1 In Gson, we can use gson.fromJson to convert JSON back to Java objects.

It works right for me. Please, provide stack trace.

catch JsonSynstaxException and e.printStackTrace();

also JsonIOException

Parse JSON file using GSON, Imo, the best way to parse your JSON response with GSON would be creating classes that "match" your response and then use Gson. Gson is a popular JSON processing library developed and maintained by Google for reading, writing, and parsing JSON data in Java. Dependencies. To add Gson to your Gradle project, add the following dependency to build.gradle file: implementation 'com.google.code.gson:gson:2.8.6' For Maven, include the below dependency to your pom.xml file: <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.6</version> </dependency> Read JSON File to a Java Map

The provided test passes, so I suggest you to catch exceptions may be little debugging helps:

@RunWith(BlockJUnit4ClassRunner.class)
public class HttpServerTest {

    @Test
    public void test() throws FileNotFoundException {

        String path = "test.json";
        BufferedReader bufferedReader = new BufferedReader(new FileReader(path));

        Gson gson = new Gson();
        JsonObject js = gson.fromJson(bufferedReader, JsonObject.class);
        String user = js.get("user").getAsString();
        assertEquals("giovanni", user);
    }

}

GSON example – Read and write JSON, Gson is a Java library that can be used to convert Java Objects into their JSON representation. In this post,we will read and write JSON using GSON. //write converted json data to a file named "CountryGSON.json". In my previous article, we looked at reading and writing JSON files in Java using different open-source libraries like JSON.simple, Jackson, Moshi, and Gson.. In this article, you'll learn how to read and write JSON using Gson in detail.

Gson Example Tutorial Parse JSON, If we want to use different name for java bean and json, then we can use @​SerializedName java  Gson is typically used by first constructing a Gson instance and then invoking toJson (Object) or fromJson (String, Class) methods on it. com.google.gson.JsonParser: A parser to parse Json into a parse tree of JsonElements com.google.gson.stream.JsonReader: Reads a JSON encoded value as a stream of tokens.

How to Read a JSON file Using GSON in Java, In this example, we use the Gson tree model API to read Java objects from JSON file. Converting java object to JSON is comparatively easy than parsing with Gson streaming API. By default JsonWriter writes json in compact form but we can set indent for pretty printing. That’s all for Gson example tutorial, let me know if you face any issues with it.

Parse and write JSON data in Java with Gson, In that tutorial, you're going to learn how to parse JSON data to Java Objects and then, how To generate Java Pojos from JSON sample data, we're going to use this code, you get a gen.json file with Java Objects converted to JSON format. Imo, the best way to parse your JSON response with GSON would be creating classes that "match" your response and then use Gson.fromJson() method.