相关文章推荐
冲动的梨子  ·  Reusable Containers ...·  1 月前    · 
深情的熊猫  ·  门指纹锁怎么设置指纹_装修问答-百安居装修网·  3 月前    · 
眼睛小的香烟  ·  中国GDP永远追不上美国?·  5 月前    · 
宽容的刺猬  ·  黄埔军校第十六期同学录(1)·  5 月前    · 
大方的台灯  ·  大疆A型板使用经验分享(五)—— ...·  11 月前    · 
Code  ›  开发:随笔记录之 Json字符串 与 List、HashMap对象的转换开发者社区
string jsonobject list hashmap
https://cloud.tencent.com/developer/article/1765090
爱健身的眼镜
2 年前
作者头像
执笔记忆的空白
0 篇文章

开发:随笔记录之 Json字符串 与 List、HashMap对象的转换

前往专栏
腾讯云
开发者社区
文档 意见反馈 控制台
首页
学习
活动
专区
工具
TVP
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
社区首页 > 专栏 > Java日常 > 开发:随笔记录之 Json字符串 与 List、HashMap对象的转换

开发:随笔记录之 Json字符串 与 List、HashMap对象的转换

作者头像
执笔记忆的空白
发布 于 2020-12-25 10:47:42
649 0
发布 于 2020-12-25 10:47:42
举报

import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import net.sf.json.JsonConfig; import net.sf.json.util.JSONUtils; public class JsonParser { * json字符串 转成 map * @param jsonStr * @return * @throws Exception public static HashMap<String, JsonValue> parse(String jsonStr) { if (jsonStr == null || "".equals(jsonStr)) { return null; } HashMap<String, JsonValue> retMap = null; try { retMap = new HashMap<String, JsonValue>(); JSONObject json = JSONObject.fromObject(jsonStr); Map<String, Object> tmpMap = (Map<String, Object>) JSONObject .toBean(json, Map.class); for (Map.Entry<String, Object> entry : tmpMap.entrySet()) { JsonValue tmp = parseRec(entry.getValue(), 0); retMap.put(entry.getKey(), tmp); catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return retMap; * json字符串 转成 List * @param jsonStr * @return * @throws Exception public static List<HashMap<String, JsonValue>> parseList(String jsonStr) { if (jsonStr == null || "".equals(jsonStr)) { return null; } List<HashMap<String, JsonValue>> retList = new ArrayList<HashMap<String, JsonValue>>(); JSONArray data = JSONArray.fromObject(jsonStr); for (int i = 0; i < data.size(); i++) { HashMap<String, JsonValue> retMap = new HashMap<String, JsonValue>(); JSONObject json = (JSONObject) data.get(i); Map<String, Object> tmpMap = (Map<String, Object>) JSONObject .toBean(json, Map.class); for (Map.Entry<String, Object> entry : tmpMap.entrySet()) { JsonValue tmp = parseRec(entry.getValue(), 0); retMap.put(entry.getKey(), tmp); retList.add(retMap); return retList; * HashMap<String, JsonValue> map 转成 json字符串 * @param jsonStr * @return * @throws Exception public static String parse(HashMap<String, JsonValue> map) { HashMap<String, Object> retMap = new HashMap<String, Object>(); for (Map.Entry<String, JsonValue> entry : map.entrySet()) { Object tmp = parseJsonValueRec(entry.getValue(), 0); retMap.put(entry.getKey(), tmp); JsonConfig jc = new JsonConfig(); return JSONObject.fromObject(retMap, jc).toString(); * List<HashMap<String, JsonValue>> list 转成 json字符串 * @param jsonStr * @return * @throws Exception public static String parse(List<HashMap<String, JsonValue>> list) { List<HashMap<String, Object>> tmpList = new ArrayList<HashMap<String, Object>>(); for (HashMap<String, JsonValue> map : list) { HashMap<String, Object> retMap = new HashMap<String, Object>(); for (Map.Entry<String, JsonValue> entry : map.entrySet()) { Object tmp = parseJsonValueRec(entry.getValue(), 0); retMap.put(entry.getKey(), tmp); tmpList.add(retMap); JSONArray json = new JSONArray(); json.addAll(tmpList); return json.toString(); * 构建json * @param map * @return public static String parse(Map map) {

 
推荐文章
冲动的梨子  ·  Reusable Containers (Experimental) - Testcontainers for Java
1 月前
深情的熊猫  ·  门指纹锁怎么设置指纹_装修问答-百安居装修网
3 月前
眼睛小的香烟  ·  中国GDP永远追不上美国?
5 月前
宽容的刺猬  ·  黄埔军校第十六期同学录(1)
5 月前
大方的台灯  ·  大疆A型板使用经验分享(五)—— DMA配置和遥控器使用_大疆dbus协议-CSDN博客
11 月前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号