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 ...
随机推荐
- CentOS7.x编译安装zabbix4.0
编译安装zabbix Zabbix简介 Zabbix 是一个企业级的分布式开源监控方案. Zabbix是一款能够监控各种网络参数以及服务器健康性和完整性的软件.Zabbix使用灵活的通知机制,允许用户 ...
- iframe知识点详解
<iframe>标签规定一个内联框架,一个内联框架被用来在当前HTML文档中嵌入另一个文档. 1. 常用属性 2. 主要API 3. 轮询 4. 长轮询 5. 自适应 6. 安全性 7. ...
- python open 函数的读写追加
- nginx 返回json格式内容
例子: #如果访问的ip是192.168.1.1,就直接返回json格式的内容 location / { default_type application/json; #####格式 if ( $re ...
- React Native & Android & iOS & APK
React Native & Android & iOS & APK https://play.google.com/apps/publish/signup/ $ 25 bui ...
- webpack基本用法
1. 压缩代码(production模式) optimization: { minimizer: [new UglifyJsPlugin({ cache: true, parallel: true, ...
- 数据分析之Numpy
Numpy numpy.array:将数组转换成向量 numpy.array([,,,]) 转化成1维向量 numpy.array([[,,],[,,],[,,]]) 转换成二维向量 vector = ...
- 获取SQL数据库中的数据库名、所有表名、所有字段名、列描述
1.获取所有数据库名: (1).Select Name FROM Master.dbo.SysDatabases orDER BY Name 2.获取所有表名: (1).Select Na ...
- 【BZOJ4002】[JLOI2015]有意义的字符串(数论,矩阵快速幂)
[BZOJ4002][JLOI2015]有意义的字符串(数论,矩阵快速幂) 题面 BZOJ 洛谷 题解 发现我这种题总是做不动... 令\(A=\frac{b+\sqrt d}{2},B=\frac{ ...
- VimFaultException A specified parameter was not correct configSpec.guestId
VimFaultException A specified parameter was not correct configSpec.guestId 在对接VMware的环境中创建虚拟机报错 查看错误 ...