In one of my previous post we have discussed about the same topic but using jackson library. For your reference - click here
In this example we are going to see how to convert java object into the json format using Google's Gson library.
Gson is pretty easy to understand and it has mainly below two methods to note.
pom.xml
Now, lets create a java test object with some initialized values. Later on we are going to convert this java object to json format using Gson library.
TestObject.java
Below is the core logic which will enable us to convert the java object to json. We are going to use the toJson() method which is available in Gson object
Java2Json.java
Along with the below output into the console we may also check for the newly created testFile.json in the location we mentioned.
Output:
In this example we are going to see how to convert java object into the json format using Google's Gson library.
Gson is pretty easy to understand and it has mainly below two methods to note.
- toJson() – Convert Java object to JSON format
- fromJson() – Convert JSON into Java object
pom.xml
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>1.7.1</version> </dependency>
Now, lets create a java test object with some initialized values. Later on we are going to convert this java object to json format using Gson library.
TestObject.java
import java.util.ArrayList; import java.util.List;
public class TestObject {
private int id = 1; private String name = "Amzi"; private List<String> skills = new ArrayList<String>() { { add("Java"); add("JSP"); add("JDBC"); } }; 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; } public List<String> getSkills() { return skills; } public void setSkills(List<String> skills) { this.skills = skills; } @Override public String toString() { return "TestObject [ID = " + id + ", name = " + name + ", skills = " + skills + "]"; } }
Below is the core logic which will enable us to convert the java object to json. We are going to use the toJson() method which is available in Gson object
Java2Json.java
package com.amzi.java;
import java.io.FileWriter; import java.io.IOException; import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class Java2Json { public static void main(String[] args) { TestObject obj = new TestObject(); //initialize Gson object //setPrettyPrinting().create() is for batter formating. Gson gson = new GsonBuilder().setPrettyPrinting().create(); // convert java object to JSON format, // and returned as JSON formatted string String json = gson.toJson(obj); try { // write converted json data to a file named "testFile.json" FileWriter writer = new FileWriter( "c:\\users\\amzi\\desktop\\testFile.json"); writer.write(json); writer.close(); } catch (IOException e) { e.printStackTrace(); } System.out.println(json); } }
Along with the below output into the console we may also check for the newly created testFile.json in the location we mentioned.
Output:
{ "id": 1, "name": "Amzi", "skills": [ "Java", "JSP", "JDBC" ] }
nice article and must visit how to convert json to java object
ReplyDeleteVery attractive and informative blog about Java... keep rocks..
ReplyDeleteJava Training in Chennai
I am really impressed with your efforts and really pleased to visit this post.
ReplyDeleteData Science training in chennai | Best Data Science training in chennai
Data Science training in OMR | Data science training in chennai
Data Science training in chennai | Best Data science Training in Chennai
Data science training in velachery | Data Science Training in Chennai
Data science training in tambaram | Data Science training in Chennai
Data Science training in anna nagar | Data science training in Chennai
This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.
ReplyDeleterpa training in chennai
rpa training in bangalore
rpa training in pune
rpa training in marathahalli
rpa training in btm
I am sure this post has helped me save many hours of browsing other related posts just to find what I was looking for. Many thanks!
ReplyDeleteSelenium training in Pune | Selenium training institute in Pune | Selenium course in Pune
Selenium Online training | Selenium Certification Online course-Gangboard
Selenium interview questions and answers
Selenium interview questions and answers
Selenium Online training | Selenium Certification Online course
It’s really great information for becoming a better Blogger. Keep sharing, Thanks...
ReplyDeleteLearn Hadoop Training from the Industry Experts we bridge the gap between the need of the industry. Softgen Infotech provide the Best Hadoop Training in Bangalore with 100% Placement Assistance. Book a Free Demo Today.
Big Data Analytics Training in Bangalore
Tableau Training in Bangalore
Data Science Training in Bangalore
Workday Training in Bangalore