java中實(shí)體類轉(zhuǎn)Json的2種方法
首先申明所需jar包:
- ezmorph-1.0.6.jar
- jackson-all-1.7.6.jar
- jsoup-1.5.2.jar
一、創(chuàng)建一個(gè)實(shí)體類Emp.
package com.hyx.entity; public class Emp { private Integer id; private String name; private Integer dptNo; private String gender; private String duty; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getDptNo() { return dptNo; } public void setDptNo(Integer dptNo) { this.dptNo = dptNo; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getDuty() { return duty; } public void setDuty(String duty) { this.duty = duty; } }
二、實(shí)體類轉(zhuǎn)換為Json
(1)
import java.io.IOException; import net.sf.json.JSONObject; import org.apache.struts2.json.JSONException; import org.codehaus.jackson.map.ObjectMapper; import com.hyx.entity.Emp; public class MainTest { public static<T> String objectToJson(T obj) throws JSONException, IOException { ObjectMapper mapper = new ObjectMapper(); // Convert object to JSON string String jsonStr = ""; try { jsonStr = mapper.writeValueAsString(obj); } catch (IOException e) { throw e; } return JSONObject.fromObject(obj).toString(); } // 主函數(shù) public static void main(String[] args) { Emp emp=new Emp(); emp.setId(1); emp.setName("張三"); emp.setGender("男"); emp.setDptNo(001); emp.setDuty("職員"); String jsonStr=""; try { jsonStr=objectToJson(emp); } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println(jsonStr); } }
(2)
import net.sf.json.JSONObject; import com.hyx.entity.Emp; public class MainTest { // 主函數(shù) public static void main(String[] args) { Emp emp=new Emp(); emp.setId(1); emp.setName("張三"); emp.setGender("男"); emp.setDptNo(001); emp.setDuty("職員"); JSONObject jsonObject = JSONObject.fromObject(emp); System.out.println(jsonObject); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis Order by動(dòng)態(tài)參數(shù)防注入方式
這篇文章主要介紹了Mybatis Order by動(dòng)態(tài)參數(shù)防注入方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04詳解SpringBoot 發(fā)布ApplicationEventPublisher和監(jiān)聽(tīng)ApplicationEvent事
這篇文章主要介紹了詳解SpringBoot 發(fā)布ApplicationEventPublisher和監(jiān)聽(tīng)ApplicationEvent事件,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-06-06springboot對(duì)數(shù)據(jù)庫(kù)密碼加密的實(shí)現(xiàn)
這篇文章主要介紹了springboot對(duì)數(shù)據(jù)庫(kù)密碼加密的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12MyBatis-Plus如何關(guān)閉SQL日志打印詳解
在使用mybatisplus進(jìn)行開(kāi)發(fā)時(shí),日志是一個(gè)非常有用的工具,它可以幫助我們更好地了解和調(diào)試我們的代碼,這篇文章主要給大家介紹了關(guān)于MyBatis-Plus如何關(guān)閉SQL日志打印的相關(guān)資料,需要的朋友可以參考下2024-03-03Spring Boot jar可執(zhí)行原理的徹底分析
這篇文章主要給大家介紹了關(guān)于Spring Boot jar可執(zhí)行原理的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07Spring mvc AJAX技術(shù)實(shí)現(xiàn)原理解析
這篇文章主要介紹了Spring mvc AJAX技術(shù)實(shí)現(xiàn)原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03