Dalam coretan kod ini, kami melakukan perkara berikut:
Buat objek Student
menggunakan kaedah penetap dan tetapkan sifatnya.
mencipta JSONObject
yang memanggil object
dan menggunakan objek Student
sebagai hujah kepada pembinanya.
JSONObject
Gunakan kaedah pengambil untuk menjana rentetan JSON.
Panggil kaedah object.toString()
untuk mendapatkan rentetan 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); } }
Menjalankan kod ini menghasilkan keputusan berikut:
{"kursus":["Kejuruteraan","Kewangan", "Kimia"],"name":"Alice","id":1,"umur":20}
{"id":1,"name":"Alice","umur":20," courses":["Kejuruteraan","Kewangan","Kimia"]}
{"id":1,"name":"Alice","umur":20,"kursus":["Kejuruteraan", "Kewangan","Kimia"]}
Kelas pelajar yang digunakan dalam kod di atas:
import java.util.List; public class Student { private Long id; private String name; private int age; private List<String> courses; public Student(Long id, String name, int age, List<String> 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<String> getCourses() { return courses; } public void setCourses(List<String> courses) { this.courses = courses; } }
Kebergantungan Maven
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example.javaobjectjson</groupId> <artifactId>JavaObjectJSON</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/org.json/json --> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20211205</version> </dependency> <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.1</version> </dependency> </dependencies> </project>
Atas ialah kandungan terperinci Bagaimana untuk menukar objek Java kepada JSON. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!