![]() |
Google GSON is a simple Java-based serialization/deserialization library to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java Object. It is a reliable, fast, and efficient extension to the Java standard library. It’s also highly optimized. Assumes Java is already installed in your local environment. Setting Up the Path for LinuxThe environment variable PATH should be properly set. If you’ve trouble doing this refer to your particular shell documentation. For example: If you use bash as your shell, then you would add the following to the end our your ‘.bashrc’ file: export PATH=$PATH:/path/to/java Setting up the Path for WindowsAssumes you have properly installed Java in C:\Program Files\java\jdk-directory Edit the ‘ C:\autoexec.bat’ file and add the following line at the end: SET PATH=%PATH%:C:\Program Files\java\jdk\bin Download GSON ArchiveDownload the latest GSON archive: https://search.maven.org/artifact/com.google.code.gson/gson/2.8.6/jar <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.6</version> </dependency> Set CLASSPATH VariableManually:
With the help of IDE: In IntelliJ IDEA: Follow the below steps Steps: Right Click on Project -> Open Module Settings -> Libraries -> Click + -> Add GSON Jar -> Apply and OK GSON ApplicationJava
Java
By default, GSON writes JSON in compact mode. {"name":"Anurag","age":18,"salary":100,"skillSet":["DevOps","Machine Learning","Open Source"]} After enabling the pretty-print mode: import com.google.gson.GsonBuilder; import java.io.FileWriter; import java.io.IOException; import java.util.Arrays; import java.util.List; public class GSONExample { public static void main(String[] args) { Gson gson = new GsonBuilder().setPrettyPrinting().create(); ... ... ... } } { "name": "Anurag", "age": 18, "salary": 100, "skillSet": [ "DevOps", "Machine Learning", "Open Source" ] } 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. |
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 9 |