java中實(shí)體類和JSON對(duì)象之間相互轉(zhuǎn)化
在需要用到JSON對(duì)象封裝數(shù)據(jù)的時(shí)候,往往會(huì)寫(xiě)很多代碼,也有很多復(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)容了,希望大家能夠喜歡。
相關(guān)文章
java?集合工具類Collections及Comparable和Comparator排序詳解
這篇文章主要介紹了java集合工具類Collections及Comparable和Comparator排序詳解,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06mybatis的坑-integer類型為0的數(shù)據(jù)if?test失效問(wèn)題
這篇文章主要介紹了mybatis的坑-integer類型為0的數(shù)據(jù)if?test失效問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01解決異常:Invalid?keystore?format,springboot配置ssl證書(shū)格式不合法問(wèn)題
這篇文章主要介紹了解決異常:Invalid?keystore?format,springboot配置ssl證書(shū)格式不合法問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03MyBatis中調(diào)用存儲(chǔ)過(guò)程和函數(shù)的實(shí)現(xiàn)示例
在MyBatis中調(diào)用存儲(chǔ)過(guò)程和函數(shù)是一個(gè)相對(duì)高級(jí)的特性,本文主要介紹了MyBatis中調(diào)用存儲(chǔ)過(guò)程和函數(shù)的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07關(guān)于HttpServletRequest獲取POST請(qǐng)求Body參數(shù)的3種方式
這篇文章主要介紹了關(guān)于HttpServletRequest獲取POST請(qǐng)求Body參數(shù)的3種方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11Spring中的Aware接口及應(yīng)用場(chǎng)景詳解
這篇文章主要介紹了Spring中的Aware接口及應(yīng)用場(chǎng)景,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01