java中實(shí)體類和JSON對(duì)象之間相互轉(zhuǎn)化
在需要用到JSON對(duì)象封裝數(shù)據(jù)的時(shí)候,往往會(huì)寫很多代碼,也有很多復(fù)制粘貼,為了用POJO的思想我們可以裝JSON轉(zhuǎn)化為實(shí)體對(duì)象進(jìn)行操作
package myUtil;
import java.io.IOException;
import myProject.Student;
import myProject.StudentList;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* 實(shí)體類和JSON對(duì)象之間相互轉(zhuǎn)化(依賴包jackson-all-1.7.6.jar、jsoup-1.5.2.jar)
* @author wck
*
*/
public class JSONUtil {
/**
* 將json轉(zhuǎn)化為實(shí)體POJO
* @param jsonStr
* @param obj
* @return
*/
public static<T> Object JSONToObj(String jsonStr,Class<T> obj) {
T t = null;
try {
ObjectMapper objectMapper = new ObjectMapper();
t = objectMapper.readValue(jsonStr,
obj);
} catch (Exception e) {
e.printStackTrace();
}
return t;
}
/**
* 將實(shí)體POJO轉(zhuǎn)化為JSON
* @param obj
* @return
* @throws JSONException
* @throws IOException
*/
public static<T> JSONObject 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 new JSONObject(jsonStr);
}
public static void main(String[] args) throws JSONException, IOException {
JSONObject obj = null;
obj = new JSONObject();
obj.put("name", "213");
obj.put("age", 27);
JSONArray array = new JSONArray();
array.put(obj);
obj = new JSONObject();
obj.put("name", "214");
obj.put("age", 28);
array.put(obj);
Student stu = (Student) JSONToObj(obj.toString(), Student.class);
JSONObject objList = new JSONObject();
objList.put("student", array);
System.out.println("objList:"+objList);
StudentList stuList = (StudentList) JSONToObj(objList.toString(), StudentList.class);
System.out.println("student:"+stu);
System.out.println("stuList:"+stuList);
System.out.println("#####################################");
JSONObject getObj = objectToJson(stu);
System.out.println(getObj);
}
}
以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
- java中常用的json,jsonarray,map數(shù)據(jù)結(jié)構(gòu)與對(duì)象互轉(zhuǎn)詳解
- Java中對(duì)象?和?json?互轉(zhuǎn)四種方式?json-lib、Gson、FastJson、Jackson
- java中json和對(duì)象之間相互轉(zhuǎn)換的運(yùn)用
- JAVA中JSONObject對(duì)象和Map對(duì)象之間的相互轉(zhuǎn)換
- Java實(shí)現(xiàn)Json字符串與Object對(duì)象相互轉(zhuǎn)換的方式總結(jié)
- 詳談Java中net.sf.json包關(guān)于JSON與對(duì)象互轉(zhuǎn)的坑
- 基于JSON和java對(duì)象的互轉(zhuǎn)方法
- java對(duì)象與json對(duì)象間的相互轉(zhuǎn)換的方法
- Java中Json字符串和Java對(duì)象的互轉(zhuǎn)
相關(guān)文章
springboot+redis 實(shí)現(xiàn)分布式限流令牌桶的示例代碼
這篇文章主要介紹了springboot+redis 實(shí)現(xiàn)分布式限流令牌桶 ,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Java實(shí)現(xiàn)簡(jiǎn)單密碼加密功能
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單密碼加密功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
Spring4.0 MVC請(qǐng)求json數(shù)據(jù)報(bào)406錯(cuò)誤的解決方法
這篇文章主要為大家詳細(xì)介紹了Spring4.0 MVC請(qǐng)求json數(shù)據(jù)報(bào)406錯(cuò)誤的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Java使用System.currentTimeMillis()方法計(jì)算程序運(yùn)行時(shí)間的示例代碼
System.currentTimeMillis() 方法的返回類型為 long ,表示毫秒為單位的當(dāng)前時(shí)間,文中通過示例代碼介紹了計(jì)算 String 類型與 StringBuilder 類型拼接字符串的耗時(shí)情況,對(duì)Java計(jì)算程序運(yùn)行時(shí)間相關(guān)知識(shí)感興趣的朋友一起看看吧2022-03-03
SpringBoot中項(xiàng)目結(jié)構(gòu)的項(xiàng)目實(shí)踐
SpringBoot項(xiàng)目結(jié)構(gòu)遵循Maven或Gradle的標(biāo)準(zhǔn)目錄結(jié)構(gòu),融入了SpringBoot的特定約定,本文就來介紹一下SpringBoot中項(xiàng)目結(jié)構(gòu)的項(xiàng)目,感興趣的可以了解一下2025-03-03
idea maven項(xiàng)目無(wú)法識(shí)別jar包里的class解決方案
這篇文章主要介紹了idea maven項(xiàng)目無(wú)法識(shí)別jar包里的class解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06

