JSON字符串与Map互转
//一、map转为json字符串
public static String map2jsonstr(Map<String,?> map){
return JSONObject.toJSONString(map);
}
//二、json字符串转Map对象
public static Map<String,?> jsonstr2map(String jsonstr){
return JSONObject.parseObject(jsonstr);
}
//三、json字符串转Map对象
public static Map<String, Object> parseJSON2Map(String jsonStr){
Map<String, Object> map = new HashMap<String, Object>();
JSONObject json = JSONObject.parseObject(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<Object> it = ((JSONArray)v).iterator();
while(it.hasNext()){
JSONObject json2 = (JSONObject)it.next();
list.add(parseJSON2Map(json2.toString()));
}
map.put(k.toString(), list);
} else {
map.put(k.toString(), v);
}
}
return map;
}
测试:
public static void main(String[] args) { try { List list = new ArrayList();
Map<String,Object> map = new HashMap<String,Object>();
list.add(1);
list.add("b");
map.put("name", "a");
map.put("age", 12);
map.put("name", "a");
map.put("list", list);
String jsonstr = map2jsonstr(map);
System.out.println("json字符串:" + jsonstr);
System.out.println("map对象"+jsonstr2map(jsonstr));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
JSON字符串与Map互转的更多相关文章
- json字符串转map
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</ar ...
- json字符串和dict互转
json字符串和dict互转 import json str = '{"params":[{"id":222,"offset":0},{&q ...
- iOS开发--字典(NSDictionary)和JSON字符串(NSString)之间互转
iOS开发--字典(NSDictionary)和JSON字符串(NSString)之间互转 1. 字典转Json字符串 // 字典转json字符串方法 -(NSString *)convertToJs ...
- json字符串转map、json数组演示
公司项目用的IBM封装的json解析,此处采用阿里的fastjson进行演示,代码如下: package com.alphajuns.test; import com.alibaba.fastjson ...
- Json字符串转map集合
第一步:在pom.xml中添加依赖; <dependency> <groupId>com.alibaba</groupId> <artifactId>f ...
- json字符串转Map、json数组
json数组转map public static void main(String[] args){ String strArr = "[{\"0\":\"zh ...
- JSON字符串转换为Map
本文是利用阿里巴巴封装的FastJSON来转换json字符串的.例子如下: package com.zkn.newlearn.json; import com.alibaba.fastjson.JSO ...
- [转]Json字符串和map和HashMap之间的转换
需要导入alibaba.fastJsonmaven中的依赖为 <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> ...
- Json字符串与字典互转
#pragma mark 转换json字符串 +(NSString *)toJSON:(id)aParam { NSData *jsonData=[NSJSONSerialization data ...
随机推荐
- 利用 keras_proprecessing.image 扩增自己的遥感数据(多波段)
1.keras 自带的 keras_proprecessing.image 只支持三种模式图片(color_mode in ['grey', 'RGB', 'RGBA'])的随机扩增. 2.遥感数据除 ...
- 基于nodejs的流水线式的CRUD服务。依赖注入可以支持插件。
写代码好多年了,发现大家的思路都是写代码.写代码.写代码,还弄了个称号——码农. 我是挺无语的,我的思路是——不写代码.不写代码.不写代码! 无聊的代码为啥要重复写呢?甚至一写写好几年. 举个例子吧, ...
- input type=file的几个属性
<input type='file' /> inputDom.onchange=function (e){ e.currentTarget.files 是只有一个对象的数组 var ob ...
- js循环获取table中的值
<script type="text/javascript"> function getTdValue() { var tableId = document.getEl ...
- linux系统docker版本升级或安装
如果存在旧版本,则先卸载 最好先将镜像导出保存,以免升级后丢失或者无法使用 如有正在运行的容器,先停止 $ docker ps -q | xargs docker stop 关闭docker服务 $ ...
- MySQL之执行流程
最近开始在学习mysql相关知识,自己根据学到的知识点,根据自己的理解整理分享出来,本篇文章会分析下一个sql语句在mysql中的执行流程,包括sql的查询在mysql内部会怎么流转,sql语句的更新 ...
- XML的几种转换
package com.qbskj.project.util; import java.io.ByteArrayOutputStream; import java.util.ArrayList; im ...
- VisualStudio神级插件Resharper技巧基础入门到骨灰玩家使用全教程+Resharper性能优化
原文地址:https://www.masuit.com/21/resharper 破解地址:https://www.masuit.com/20/resharper 官方文档:https://www.j ...
- [2019.03.21]LF, CR, CRLF and LFCR(?)
开玩笑的啦,没有LFCR这种沙雕东西 为什么突然想起来写这个呢,是因为先前照着shell画llehs的时候,总报错,改正了以后又因为看不见而在上一篇博客上没有写明,所以过来好好写一写咯. 可以看出报错 ...
- 最简单的JAVA解析XML字符串方法
引入 dom4j 包<dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifa ...