使用 Jackson API 将Java对象转换为 Json 字符串
JSON 代表JavaScript 对象表示法。它是一种基于文本的标准格式,显示基于 JavaScript 对象语法的结构化数据。它通常用于在 Web 应用程序中传输数据。强烈建议使用 JSON 在服务器和 Web 应用程序之间传输数据。为了将Java对象转换为 JSON,可以使用以下两种方法,如下所示:
- GSON
- 杰克逊 API
使用 Jackson API 将Java对象转换为 JSON。
将Java对象转换为 JSON 字符串的步骤
第一步:添加Jackson的jar文件( Maven项目在pom.xml文件中添加Jackson依赖)
html
com.fasterxml.jackson.core
jackson-databind
2.5.3
Java
// Java Program to Illustrate Organisation Class
package com.Geeks;
// Importing required clases
public class Organisation {
// Class data members
private String organisation_name;
private String description;
private int Employees;
// Calling getters and setters
// Getter
public String getOrganisation_name()
{
return organisation_name;
}
// Setter
public void
setOrganisation_name(String organisation_name)
{
this.organisation_name = organisation_name;
}
// Getter
public String getDescription() { return description; }
// Setter
public void setDescription(String description)
{
this.description = description;
}
// Getter
public int getEmployees() { return Employees; }
// Setter
public void setEmployees(int employees)
{
Employees = employees;
}
// Method
// Creating toString
@Override public String toString()
{
// Returning attributes of organisation
return "Organisation [organisation_name="
+ organisation_name
+ ", description=" + description
+ ", Employees=" + Employees + "]";
}
}
Java
// Java Program to Illustrate Object to JSON Conversion
package com.Geeks;
// Importing required classes
import com.Geeks.Organisation;
import java.io.IOException;
import org.codehaus.jackson.map.ObjectMapper;
// Class
public class ObjectToJson {
// Main driver method
public static void main(String[] a)
{
// Creating object of Organisation
Organisation org = new Organisation();
// Insert the data into the object
org = getObjectData(org);
// Creating Object of ObjectMapper define in Jackson
// Api
ObjectMapper Obj = new ObjectMapper();
// Try block to check for exceptions
try {
// Getting organisation object as a json string
String jsonStr = Obj.writeValueAsString(org);
// Displaying JSON String on console
System.out.println(jsonStr);
}
// Catch block to handle exceptions
catch (IOException e) {
// Display exception along with line number
// using printStackTrace() method
e.printStackTrace();
}
}
// Method
// Getting the data to be inserted
// into the object
public static Organisation
getObjectData(Organisation org)
{
// Insert the custom data
org.setName("GeeksforGeeks");
org.setDescription(
"A computer Science portal for Geeks");
org.setEmployees(2000);
// Returning the object
return org;
}
现在 pom.xml 文件如下所示:
文件:极客/pom.xml
第 2 步:创建一个 POJO(Plain Old Java Object)以转换为 JSON
Java
// Java Program to Illustrate Organisation Class
package com.Geeks;
// Importing required clases
public class Organisation {
// Class data members
private String organisation_name;
private String description;
private int Employees;
// Calling getters and setters
// Getter
public String getOrganisation_name()
{
return organisation_name;
}
// Setter
public void
setOrganisation_name(String organisation_name)
{
this.organisation_name = organisation_name;
}
// Getter
public String getDescription() { return description; }
// Setter
public void setDescription(String description)
{
this.description = description;
}
// Getter
public int getEmployees() { return Employees; }
// Setter
public void setEmployees(int employees)
{
Employees = employees;
}
// Method
// Creating toString
@Override public String toString()
{
// Returning attributes of organisation
return "Organisation [organisation_name="
+ organisation_name
+ ", description=" + description
+ ", Employees=" + Employees + "]";
}
}
第 3 步:创建一个用于将组织对象转换为 JSON 的Java类。
使用 Jackson API 的 ObjectMapper 类将对象转换为 JSON。
Java
// Java Program to Illustrate Object to JSON Conversion
package com.Geeks;
// Importing required classes
import com.Geeks.Organisation;
import java.io.IOException;
import org.codehaus.jackson.map.ObjectMapper;
// Class
public class ObjectToJson {
// Main driver method
public static void main(String[] a)
{
// Creating object of Organisation
Organisation org = new Organisation();
// Insert the data into the object
org = getObjectData(org);
// Creating Object of ObjectMapper define in Jackson
// Api
ObjectMapper Obj = new ObjectMapper();
// Try block to check for exceptions
try {
// Getting organisation object as a json string
String jsonStr = Obj.writeValueAsString(org);
// Displaying JSON String on console
System.out.println(jsonStr);
}
// Catch block to handle exceptions
catch (IOException e) {
// Display exception along with line number
// using printStackTrace() method
e.printStackTrace();
}
}
// Method
// Getting the data to be inserted
// into the object
public static Organisation
getObjectData(Organisation org)
{
// Insert the custom data
org.setName("GeeksforGeeks");
org.setDescription(
"A computer Science portal for Geeks");
org.setEmployees(2000);
// Returning the object
return org;
}
第三步:执行流程。
JSON 中的输出将如下所示:
输出: