Java Json的各種處理
一、net.sf.json
1、Json轉Map
JSONObject jsonObject = JSONObject.fromObject(jsonStr); Map<String,Object> map = new HashMap<>(); map.put("code",jsonObject .getInt("code"));
2、Json轉實體
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轉集合
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轉Map
JSONObject jsonObject = JSON.parseObject(jsonStr); Map<String,Object> map = new HashMap<>(); map.put("code",jsonObject .getInt("code"));
2、Json轉實體
ArticleForm articleForm = JSON.parseObject(jsonStr, new TypeReference<ArticleForm>() {});
3、Json轉集合
List<ArticleForm> list = JSON.parseObject(jsonStr,new TypeReference<ArrayList<ArticleForm>>() {});
Java常用json處理
// String和json的互相转换 String str = "{\"status\":200,\"message\":\"\",\"data\":{\"KmList\":[\"总分\",\"语文\",\"数学\",\"英语\",\"道德与法治\",\"科学基础\"]}}"; System.out.println("str:"+str); // JSONArray arrays = JSON.parseArray(str); // string转jsonArray JSONObject jsonObject = JSON.parseObject(str); // string转jsonObject System.out.println("jsonObject:"+jsonObject); String s = jsonObject.toJSONString(); // json(object和Array相同)转string // json转list<Object>或者object String str1 = "[\"总分\",\"语文\",\"数学\",\"英语\",\"道德与法治\",\"科学基础\"]"; List<String> list = JSON.parseArray(str1, String.class); // json转list集合,将String.class改成其他对象.class即可 System.out.println("list:"+JSON.toJSONString(list)); String s1 = JSON.parseObject(JSON.toJSONString("语文"), String.class); // json转对象,将String.class改成其他对象.class即可 System.out.println("s1:"+s1); // object转字符串后即可转jsonObject或者jsonArray // json和map Map<String, Object> map = new HashMap<>(); map.put("xAxis","11"); map.put("yAxis","2222"); String json = JSON.toJSONString(map);//map转String System.out.println("json:"+json); Map<String, Object> map1 = JSON.parseObject(json, Map.class); // 转List<Map> parserArray即可 System.out.println("map1:"+map1);
以上是Java中Json的處理方法有哪些的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3漢化版
中文版,非常好用

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能