溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么將Java對象轉(zhuǎn)換為JSON

發(fā)布時(shí)間:2022-08-12 10:08:37 來源:億速云 閱讀:153 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“怎么將Java對象轉(zhuǎn)換為JSON”,在日常操作中,相信很多人在怎么將Java對象轉(zhuǎn)換為JSON問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么將Java對象轉(zhuǎn)換為JSON”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

在此代碼段中,我們執(zhí)行以下操作:

  • 使用 setter 方法創(chuàng)建Student對象并設(shè)置其屬性。

  • 創(chuàng)建JSONObject調(diào)用object并將Student對象用作其構(gòu)造函數(shù)的參數(shù)。

  • JSONObject使用 getter 方法生成 JSON 字符串。

  • 調(diào)用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);
 
    }
}

運(yùn)行此代碼會(huì)產(chǎn)生以下結(jié)果:

{"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<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;
    }
 
}

Maven 依賴項(xiàng)

<?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>

到此,關(guān)于“怎么將Java對象轉(zhuǎn)換為JSON”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI