Home > Java > javaTutorial > What is the difference between Gson's fromJson() and toJson() methods in Java?

What is the difference between Gson's fromJson() and toJson() methods in Java?

PHPz
Release: 2023-08-19 18:33:14
forward
1285 people have browsed it

What is the difference between Gsons fromJson() and toJson() methods in Java?

Gson is a library for Java that can be used to generate JSON. We can use Gson's fromJson() method to parse a JSON string into a Java object and use Gson's toJson() method to convert a Java object into a JSON string. The fromJson() method has two parameters. The first parameter is the JSON string to be parsed, and the second parameter is the Java class to be parsed. We can pass one argument to the toJson() method, which is the Java object we want to convert to a JSON string.

The syntax of fromJson()

public <T> fromJson(java.lang.String json, java.lang.Class<T> classOfT) throws JsonSyntaxException
Copy after login

Example

import com.google.gson.*;
public class FromJsonMethodTest {
   public static void main(String[] args) {
      String jsonString = "{&#39;id&#39;:101, &#39;firstName&#39;:&#39;Jai&#39;,&#39;lastName&#39;:&#39;Adithya&#39;}";
      <strong>Gson </strong>gson = new Gson();
      Employee emp = gson.fromJson(jsonString, Employee.class);
      System.out.println(emp);
   }
}
// Employee class<strong>
</strong>class Employee {
   private int id;
   private String firstName;
   private String lastName;
   public Employee() {}
   public int getId() {
      return id;
   }
   public void setId(int id) {
      this.id = id;
   }
   public String getFirstName() {
      return firstName;
   }
   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }
   public String getLastName() {
      return lastName;
   }
   public void setLastName(String lastName) {
      this.lastName = lastName;
   }
   @Override
   public String toString() {
      StringBuilder sb = new StringBuilder();
      sb.append("Id : " + id);
      sb.append(", FirstName : " + firstName);
      sb.append(", Last Name : " + lastName);
      return sb.toString();
   }
}
Copy after login

Output

Id : 101, FirstName : Jai, Last Name : Adithya<strong>
</strong>
Copy after login

toJson() Syntax

public java.lang.String toJson(java.lang.Object src)
Copy after login

Example

import com.google.gson.*;
public class ToJsonMethodTest {
   public static void main(String[] args) {
      Employee emp = new Employee();
      emp.setId(110);
      emp.setFirstName("Raja");
      emp.setLastName("Ramesh");
      Gson gson = new Gson();
      String jsonString = gson.toJson(emp);
      System.out.println(jsonString);
   }
}
// Employee class<strong>
</strong>class Employee {
   private int id;
   private String firstName;
   private String lastName;
   public Employee() {}
   public int getId() {
      return id;
   }
   public void setId(int id) {
      this.id = id;
   }
   public String getFirstName() {
      return firstName;
   }
   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }
   public String getLastName() {
      return lastName;
   }
   public void setLastName(String lastName) {
      this.lastName = lastName;
   }
}
Copy after login

Output

{"id":110,"firstName":"Raja","lastName":"Ramesh"}
Copy after login

The above is the detailed content of What is the difference between Gson's fromJson() and toJson() methods in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template