關(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)實體
JSONObject jsonObject = JSONObject.fromObject(jsonStr); ArticleForm articleForm = (ArticleForm) JSONObject.toBean(jsonObject , ArticleForm.class);
如果實體中帶有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)實體
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\":[\"總分\",\"語文\",\"數(shù)學(xué)\",\"英語\",\"道德與法治\",\"科學(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 = "[\"總分\",\"語文\",\"數(shù)學(xué)\",\"英語\",\"道德與法治\",\"科學(xué)基礎(chǔ)\"]"; List<String> list = JSON.parseArray(str1, String.class); // json轉(zhuǎn)list集合,將String.class改成其他對象.class即可 System.out.println("list:"+JSON.toJSONString(list)); String s1 = JSON.parseObject(JSON.toJSONString("語文"), String.class); // json轉(zhuǎn)對象,將String.class改成其他對象.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);
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Socket結(jié)合線程池使用實現(xiàn)客戶端和服務(wù)端通信demo
這篇文章主要為大家介紹了Socket結(jié)合線程池的使用來實現(xiàn)客戶端和服務(wù)端通信實戰(zhàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03解決MyEclipse6.5無法啟動,一直停留剛開始啟動界面的詳解
本篇文章是對解決MyEclipse6.5無法啟動,一直停留剛開始啟動界面的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05SpringBoot中添加監(jiān)聽器及創(chuàng)建線程的代碼示例
這篇文章主要介紹了SpringBoot中如何添加監(jiān)聽器及創(chuàng)建線程,文中有詳細(xì)的代碼示例,具有一定的參考價值,需要的朋友可以參考下2023-06-06Java 中 synchronized的用法詳解(四種用法)
Java語言的關(guān)鍵字,當(dāng)它用來修飾一個方法或者一個代碼塊的時候,能夠保證在同一時刻最多只有一個線程執(zhí)行該段代碼。本文給大家介紹java中 synchronized的用法,對本文感興趣的朋友一起看看吧2015-11-11springboot項目實現(xiàn)斷點續(xù)傳功能
這篇文章主要介紹了springboot項目實現(xiàn)斷點續(xù)傳,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08IDEA SpringBoot:Cannot resolve configuration&
這篇文章主要介紹了IDEA SpringBoot:Cannot resolve configuration property配置文件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07Java中用enum結(jié)合testng實現(xiàn)數(shù)據(jù)驅(qū)動的方法示例
TestNG數(shù)據(jù)驅(qū)動提供的參數(shù)化讓我們在測試項目可以靈活根據(jù)需求建立不同的dataprovider來提供數(shù)據(jù),而真正實現(xiàn)數(shù)據(jù),頁面,測試彼此獨立而又有機結(jié)合的可能性。 下面這篇文章主要給大家介紹了Java中用enum和testng做數(shù)據(jù)驅(qū)動的方法示例,需要的朋友可以參考借鑒。2017-01-01