關(guān)于Java中Json的各種處理
Java Json的各種處理
一、net.sf.json
1、Json轉(zhuǎn)Map
JSONObject jsonObject = JSONObject.fromObject(jsonStr); Map<String,Object> map = new HashMap<>(); map.put("code",jsonObject .getInt("code"));
2、Json轉(zhuǎn)實(shí)體
JSONObject jsonObject = JSONObject.fromObject(jsonStr); ArticleForm articleForm = (ArticleForm) JSONObject.toBean(jsonObject , ArticleForm.class);
如果實(shí)體中帶有List字段,需要指定泛型
Map<String, Class> classMap = new HashMap<String, Class>(); classMap.put("keywords", String.class); ArticleForm articleForm = (ArticleForm) JSONObject.toBean(data.getJSONObject(i), ArticleForm.class,classMap);
3、Json轉(zhuǎn)集合
List<ArticleForm> list = new ArrayList<>(); JSONArray data = jsonObject.getJSONArray("data"); if (errorCode == 0 && data != null && !data.isEmpty()) { ?? ?for (int i = 0; i < data.size(); i++) { ?? ??? ?Map<String, Class> classMap = new HashMap<String, Class>(); ?? ??? ?classMap.put("keywords", String.class); ?? ??? ?ArticleForm articleForm = (ArticleForm) JSONObject.toBean(data.getJSONObject(i), ArticleForm.class,classMap); ?? ??? ?list.add(articleForm); ?? ?} }
另外一種:
List<ArticleForm> list = new ArrayList<>(); JSONArray data = jsonObject.getJSONArray("data"); if (errorCode == 0 && data != null && !data.isEmpty()) { ?? ?Map<String, Class> classMap = new HashMap<String, Class>(); ?? ?classMap.put("keywords", String.class); ?? ?list ?= (List<ArticleForm>) JSONArray.toArray(data, ArticleForm.class,classMap); }
二、com.alibaba.fastjson
1、Json轉(zhuǎn)Map
JSONObject jsonObject = JSON.parseObject(jsonStr); Map<String,Object> map = new HashMap<>(); map.put("code",jsonObject .getInt("code"));
2、Json轉(zhuǎn)實(shí)體
ArticleForm articleForm = JSON.parseObject(jsonStr, new TypeReference<ArticleForm>() {});
3、Json轉(zhuǎn)集合
List<ArticleForm> list = JSON.parseObject(jsonStr,new TypeReference<ArrayList<ArticleForm>>() {});
Java常用json處理
// String和json的互相轉(zhuǎn)換 String str = "{\"status\":200,\"message\":\"\",\"data\":{\"KmList\":[\"總分\",\"語(yǔ)文\",\"數(shù)學(xué)\",\"英語(yǔ)\",\"道德與法治\",\"科學(xué)基礎(chǔ)\"]}}"; System.out.println("str:"+str); // JSONArray arrays = JSON.parseArray(str); // string轉(zhuǎn)jsonArray JSONObject jsonObject = JSON.parseObject(str); // string轉(zhuǎn)jsonObject System.out.println("jsonObject:"+jsonObject); String s = jsonObject.toJSONString(); // json(object和Array相同)轉(zhuǎn)string // json轉(zhuǎn)list<Object>或者object String str1 = "[\"總分\",\"語(yǔ)文\",\"數(shù)學(xué)\",\"英語(yǔ)\",\"道德與法治\",\"科學(xué)基礎(chǔ)\"]"; List<String> list = JSON.parseArray(str1, String.class); // json轉(zhuǎn)list集合,將String.class改成其他對(duì)象.class即可 System.out.println("list:"+JSON.toJSONString(list)); String s1 = JSON.parseObject(JSON.toJSONString("語(yǔ)文"), String.class); // json轉(zhuǎn)對(duì)象,將String.class改成其他對(duì)象.class即可 System.out.println("s1:"+s1); // object轉(zhuǎn)字符串后即可轉(zhuǎn)jsonObject或者jsonArray // json和map Map<String, Object> map = new HashMap<>(); map.put("xAxis","11"); map.put("yAxis","2222"); String json = JSON.toJSONString(map);//map轉(zhuǎn)String System.out.println("json:"+json); Map<String, Object> map1 = JSON.parseObject(json, Map.class); // 轉(zhuǎn)List<Map> parserArray即可 System.out.println("map1:"+map1);
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Socket結(jié)合線程池使用實(shí)現(xiàn)客戶端和服務(wù)端通信demo
這篇文章主要為大家介紹了Socket結(jié)合線程池的使用來(lái)實(shí)現(xiàn)客戶端和服務(wù)端通信實(shí)戰(zhàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03解決MyEclipse6.5無(wú)法啟動(dòng),一直停留剛開始啟動(dòng)界面的詳解
本篇文章是對(duì)解決MyEclipse6.5無(wú)法啟動(dòng),一直停留剛開始啟動(dòng)界面的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05SpringBoot中添加監(jiān)聽器及創(chuàng)建線程的代碼示例
這篇文章主要介紹了SpringBoot中如何添加監(jiān)聽器及創(chuàng)建線程,文中有詳細(xì)的代碼示例,具有一定的參考價(jià)值,需要的朋友可以參考下2023-06-06Java 中 synchronized的用法詳解(四種用法)
Java語(yǔ)言的關(guān)鍵字,當(dāng)它用來(lái)修飾一個(gè)方法或者一個(gè)代碼塊的時(shí)候,能夠保證在同一時(shí)刻最多只有一個(gè)線程執(zhí)行該段代碼。本文給大家介紹java中 synchronized的用法,對(duì)本文感興趣的朋友一起看看吧2015-11-11springboot項(xiàng)目實(shí)現(xiàn)斷點(diǎn)續(xù)傳功能
這篇文章主要介紹了springboot項(xiàng)目實(shí)現(xiàn)斷點(diǎn)續(xù)傳,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08深入理解Java設(shè)計(jì)模式之狀態(tài)模式
這篇文章主要介紹了JAVA設(shè)計(jì)模式之職責(zé)鏈模式的的相關(guān)資料,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解2021-11-11IDEA SpringBoot:Cannot resolve configuration&
這篇文章主要介紹了IDEA SpringBoot:Cannot resolve configuration property配置文件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07JDBC使用Statement修改數(shù)據(jù)庫(kù)
這篇文章主要為大家詳細(xì)介紹了JDBC使用Statement修改數(shù)據(jù)庫(kù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08Java中用enum結(jié)合testng實(shí)現(xiàn)數(shù)據(jù)驅(qū)動(dòng)的方法示例
TestNG數(shù)據(jù)驅(qū)動(dòng)提供的參數(shù)化讓我們?cè)跍y(cè)試項(xiàng)目可以靈活根據(jù)需求建立不同的dataprovider來(lái)提供數(shù)據(jù),而真正實(shí)現(xiàn)數(shù)據(jù),頁(yè)面,測(cè)試彼此獨(dú)立而又有機(jī)結(jié)合的可能性。 下面這篇文章主要給大家介紹了Java中用enum和testng做數(shù)據(jù)驅(qū)動(dòng)的方法示例,需要的朋友可以參考借鑒。2017-01-01