java中實體類和JSON對象之間相互轉(zhuǎn)化
更新時間:2015年05月11日 11:10:16 投稿:hebedich
Java中關(guān)于Json格式轉(zhuǎn)化Object,Map,Collection類型和String類型之間的轉(zhuǎn)化在我們實際項目中應(yīng)用的很是普遍和廣泛。最近工作的過程中也是經(jīng)常有,因此,自己封裝了一個類分享給大家。
在需要用到JSON對象封裝數(shù)據(jù)的時候,往往會寫很多代碼,也有很多復(fù)制粘貼,為了用POJO的思想我們可以裝JSON轉(zhuǎ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;
/**
* 實體類和JSON對象之間相互轉(zhuǎn)化(依賴包jackson-all-1.7.6.jar、jsoup-1.5.2.jar)
* @author wck
*
*/
public class JSONUtil {
/**
* 將json轉(zhuǎn)化為實體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;
}
/**
* 將實體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);
}
}
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
您可能感興趣的文章:
- java中常用的json,jsonarray,map數(shù)據(jù)結(jié)構(gòu)與對象互轉(zhuǎn)詳解
- Java中對象?和?json?互轉(zhuǎn)四種方式?json-lib、Gson、FastJson、Jackson
- java中json和對象之間相互轉(zhuǎn)換的運用
- JAVA中JSONObject對象和Map對象之間的相互轉(zhuǎn)換
- Java實現(xiàn)Json字符串與Object對象相互轉(zhuǎn)換的方式總結(jié)
- 詳談Java中net.sf.json包關(guān)于JSON與對象互轉(zhuǎn)的坑
- 基于JSON和java對象的互轉(zhuǎn)方法
- java對象與json對象間的相互轉(zhuǎn)換的方法
- Java中Json字符串和Java對象的互轉(zhuǎn)
相關(guān)文章
springboot+redis 實現(xiàn)分布式限流令牌桶的示例代碼
這篇文章主要介紹了springboot+redis 實現(xiàn)分布式限流令牌桶 ,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04
Spring4.0 MVC請求json數(shù)據(jù)報406錯誤的解決方法
這篇文章主要為大家詳細介紹了Spring4.0 MVC請求json數(shù)據(jù)報406錯誤的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01
Java使用System.currentTimeMillis()方法計算程序運行時間的示例代碼
System.currentTimeMillis() 方法的返回類型為 long ,表示毫秒為單位的當前時間,文中通過示例代碼介紹了計算 String 類型與 StringBuilder 類型拼接字符串的耗時情況,對Java計算程序運行時間相關(guān)知識感興趣的朋友一起看看吧2022-03-03
SpringBoot中項目結(jié)構(gòu)的項目實踐
SpringBoot項目結(jié)構(gòu)遵循Maven或Gradle的標準目錄結(jié)構(gòu),融入了SpringBoot的特定約定,本文就來介紹一下SpringBoot中項目結(jié)構(gòu)的項目,感興趣的可以了解一下2025-03-03
idea maven項目無法識別jar包里的class解決方案
這篇文章主要介紹了idea maven項目無法識別jar包里的class解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06

