Map和JSON的互相转换
JSON-Lib方式
/**
* 函数注释:parseJSON2Map()<br>
* 用途:该方法用于json数据转换为<Map<String, Object>
* @param jsonStr
* @return
*/
public static Map<String, Object> parseJSON2Map(String jsonStr){
Map<String, Object> map = new HashMap<String, Object>();
//最外层解析
JSONObject json = JSONObject.fromObject(jsonStr);
for(Object k : json.keySet()){
Object v = json.get(k);
//如果内层还是数组的话,继续解析
if(v instanceof JSONArray){
List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
Iterator<JSONObject> it = ((JSONArray)v).iterator();
while(it.hasNext()){
JSONObject json2 = it.next();
list.add(parseJSON2Map(json2.toString()));
}
map.put(k.toString(), list);
} else {
map.put(k.toString(), v);
}
}
return map;
}
/**
* 函数注释:parseJSON2MapString()<br>
* 用途:该方法用于json数据转换为<Map<String, String><br>
*/
public static Map<String, String> parseJSON2MapString(String jsonStr){
Map<String, String> map = new HashMap<String, String>();
//最外层解析
JSONObject json = JSONObject.fromObject(jsonStr);
for(Object k : json.keySet()){
Object v = json.get(k);
if(null!=v){
map.put(k.toString(), v.toString());
}
}
return map;
}
Jackson方式
/**
* Map转换JSON
* @param map
* @return
* @throws Exception
*/
public static String mapConvert2JSON(Map map) throws Exception{
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(map);
} /**
* JSON 转换 Map
* @param json
*/
public static Map readJson2Map(String json) throws Exception{
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Map<String, Object>> maps = objectMapper.readValue(json, Map.class);
/*Set<String> key = maps.keySet();
Iterator<String> iter = key.iterator();
while (iter.hasNext()) {
String field = iter.next();
System.out.println(field + ":" + maps.get(field));
}*/
return maps;
}
本段代码来自 http://www.ibloger.net/article/2470.html
Map和JSON的互相转换的更多相关文章
- map和json之间的转换
Action中在session中存放了一个map<String,Object>,跳转到a.jsp,a.jsp通过form提交到BAction,BAction可从session中获得map值 ...
- Java处理JSON的工具类(List、Map和JSON之间的转换)——依赖jsonlib支持Map嵌套
原文链接:http://www.itjhwd.com/java_json/ 代码 package com.itjh.mmp.util; import java.io.BufferedReader; i ...
- Go语言高级特性总结——Struct、Map与JSON之间的转化
Struct与Map之间互相转换 // Struct2Map convert struct to map func Struct2Map(st interface{}) map[string]inte ...
- [转]Json字符串和map和HashMap之间的转换
需要导入alibaba.fastJsonmaven中的依赖为 <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> ...
- 将java中Map对象转为有相同属性的类对象(json作为中间转换)
java中Map对象转为有相同属性的类对象(json作为中间转换) 准备好json转换工具类 public class JsonUtil { private static ObjectMapper o ...
- ES6中Json、String、Map、Object之间的转换
/** *字符串转json * */ static stringToJson(data){ return JSON.parse(data); } /** *json转字符串 */ static jso ...
- Java中把对象、对象bean、list集合、对象数组、Map和Set以及字符串转换成Json
对象转换为Json 对象bean转换为Json List集合转换为Json 对象数组转换为Json Map集合转换为Json Set集合转为Json 字符串转换为Json 把Java对常用的一些数据转 ...
- java bean、List、数组、map和Json的相互转化
工程 json包为 代码 package com.my.json; public class ChildBean { private String childName; private String ...
- Android项目开发全程(四)-- 将网络返回的json字符串轻松转换成listview列表
前面几篇博文介绍了从项目搭建到获取网络字符串,对一个项目的前期整体工作进行了详细的介绍,本篇接着上篇介绍一下怎么样优雅将网络返回的json字符串轻松转换成listview列表. 先上图,看一下效果. ...
随机推荐
- 微信小程序 - 自适应swiper高度(非组件)
微信小程序swiper默认高度375rpx,一旦超过这高度,就滑动不到内容了,我们利用css3可以很简单做到这件事情 原理: 利用css3 横轴滚动属性overflow:scroll,设置死swipe ...
- Cocon90.Db调用方法
Cocon90.DB 使用说明 开源库:https://github.com/Cocon90/Cocon90.Db Sqlite位置:https://www.nuget.org/packages/Co ...
- vs2015使用Apache Cordova用JavaScript来访问本地设备的功能,比如摄像头、加速计
看到下面这张图就代表着我VS2015 跨平台Moblie开发工具安装成功了. 上周安装成功后本想一睹跨平台开发的乐趣,可是一直找不到合适的入口.这周又来捯饬一下结果发现了一个入口.于是来写一个Hell ...
- Struts2学习笔记二:开发流程
一:创建项目,添加依赖包 二:在web.xml配置核心控制器 <filter> <filter-name>struts2</filter-name> <fil ...
- JNI 引用问题梳理(转)
局部引用: JNI 函数内部创建的 jobject 对象及其子类( jclass . jstring . jarray 等) 对象都是局部引用,它们在 JNI 函数返回后无效: 一般情况下,我们应该依 ...
- Number of dynamic partitions exceeded hive.exec.max.dynamic.partitions.pernode
动态分区数太大的问题:[Fatal Error] Operator FS_2 (id=2): Number of dynamic partitions exceeded hive.exec.max.d ...
- soapUI pro :INFO:Error getting response for []; javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
need to configure two for the https address: Step 1 export the certificate from the IE settings opti ...
- 【DB2】查看表空间对应的物理文件地址
使用的命令: db2 list tablespaces show detail db2 list tablespace containers for [Tablespace ID] [show det ...
- ROS知识(15)----Actionlib的使用(一)
Actionlib是ROS非常重要的库,像执行各种运动的动作,例如控制手臂去抓取一个杯子,这个过程可能复杂而漫长,执行过程中还可能强制中断或反馈信息,这时Actionlib就能大展伸手了. 1.原理 ...
- 要想找出包含“w”的名字
要想找出包含“w”的名字:mysql> SELECT * FROM pet WHERE name LIKE '%w%'“_”:匹配任何单个字符“%”:匹配任意数目字符(包括零字符)