如何将Java对象转换为JSON实例详解

要将 Java 对象或 POJO (普通旧 Java 对象)转换为 JSON,我们可以使用JSONObject将对象作为参数的构造函数之一。在下面的示例中,我们将StudentPOJO 转换为 JSON 字符串。Student类必须提供 getter 方法,JSONObject通过调用这些方法创建 JSON 字符串。

在此代码段中,我们执行以下操作:

  • 使用 setter 方法创建Student对象并设置其属性。
  • 创建JSONObject调用object并将Student对象用作其构造函数的参数。
  • JSONObject使用 getter 方法生成 JSON 字符串。
  • 调用object.toString()方法获取 JSON 字符串。
 
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import org.json.JSONObject;
 
import java.util.Arrays;
 
public class PojoToJSON {
 
    public static void main(String[] args) throws JsonProcessingException {
        Student student = new Student();
        student.setId(1L);
        student.setName("Alice");
        student.setAge(20);
        student.setCourses(Arrays.asList("Engineering", "Finance", "Chemistry"));
 
        JSONObject object = new JSONObject(student);
        String json = object.toString();
        System.out.println(json);
        System.out.println(new Gson().toJson(student));
        // Creating Object of ObjectMapper define in Jackson API
        ObjectMapper Obj = new ObjectMapper();
 
        // Converting the Java object into a JSON string
        String jsonStr = Obj.writeValueAsString(student);
        // Displaying Java object into a JSON string
        System.out.println(jsonStr);
 
    }
}

运行此代码会产生以下结果:

{"courses":["Engineering","Finance","Chemistry"],"name":"Alice","id":1,"age":20}
{"id":1,"name":"Alice","age":20,"courses":["Engineering","Finance","Chemistry"]}
{"id":1,"name":"Alice","age":20,"courses":["Engineering","Finance","Chemistry"]}

上面代码中使用的Student类:

 
import java.util.List;
 
public class Student {
 
    private Long id;
    private String name;
    private int age;
    private List courses;
 
    public Student(Long id, String name, int age, List courses) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.courses = courses;
    }
 
    Student() {
 
    }
 
    public Long getId() {
        return id;
    }
 
    public void setId(Long id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
 
    public List getCourses() {
        return courses;
    }
 
    public void setCourses(List courses) {
        this.courses = courses;
    }
 
}

Maven 依赖项



    4.0.0
    com.example.javaobjectjson
    JavaObjectJSON
    1.0-SNAPSHOT
    
 
        
        
            org.json
            json
            20211205
        
        
        
            com.google.code.gson
            gson
            2.9.0
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.12.1
          
    
    

总结

到此这篇关于如何将Java对象转换为JSON的文章就介绍到这了,更多相关Java对象转换JSON内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(如何将Java对象转换为JSON实例详解)