String to JSON Java
In this post, you will learn how to convert string to JSON using the Java programming language.
In the development process, we may face some scenarios where there may be a need to perform a data conversion process. A string is a sequence of characters. Java strings are created and manipulated through the string class. Strings are immutable, indexable, and iterable.
JSON (JavaScript Object Notation) is a lightweight, open standard file format. It is an array data type, consisting of attribute-value pairs. It is easy for humans to read and write. It is used primarily to transmit data between a web application and a server. JSON is popular among developers for data serialization. It is so popular that every modern programming language has methods to generate and parse JSON formatted data.
Java Gson Library
Java provides various library to convert a string to JSON. Here we have used the Gson library. Gson is an open-source Java library to serialize and deserialize Java objects to JSON. Before using this library, we need to set the classpath of gson.jar.
Java Gson LibraryGson is the main class for using Gson library.
Gson gson = new Gson();
Convert Java string to JSON using Gson library
import com.google.gson.*;
class Company {
int emp_id;
String first_name;
String last_name;
String department;
char gender;
public Company()
{
this.emp_id = 0;
this.first_name = "";
this.last_name = "";
this.department = "";
this.gender = ' ';
}
}
public class JsonClass {
public static void main(String[] args) {
Company emp = null;
// creating JSON String of GFG class object
String jsonString;
jsonString = "{";
jsonString += "emp_id : 1242,";
jsonString += "first_name : 'Andrew',";
jsonString += "last_name : 'Smith',";
jsonString += "department : 'Sales',";
jsonString += "gender : 'M'";
jsonString += "}";
// creating object of gson
Gson gson = new Gson();
// converting jsonString into object
emp = gson.fromJson(jsonString, Company.class);
System.out.println("Employee ID : " + emp.emp_id);
System.out.println("First Name : " + emp.first_name);
System.out.println("Last Name : " + emp.last_name);
System.out.println("Department : " + emp.department);
System.out.println("Gender : " + emp.gender);
}
}
Output of the above code:
Employee ID : 1242
First Name : Andrew
Last Name : Smith
Department : Sales
Gender : M
Related Articles
Parse JSON in javaJava write data to JSON file
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number