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 ...
随机推荐
- php类注释生成接口文档
参考地址:https://github.com/itxq/api-doc-php
- iframe知识点详解
<iframe>标签规定一个内联框架,一个内联框架被用来在当前HTML文档中嵌入另一个文档. 1. 常用属性 2. 主要API 3. 轮询 4. 长轮询 5. 自适应 6. 安全性 7. ...
- [Alpha阶段]测试报告
[Alpha]阶段测试报告 在测试过程中发现的BUG 在最后的测试阶段中,我们不可避免的遇到了各种各样的BUG.虽然大多数都不是严重的BUG,但是这些细枝末节的问题的堆积,依然会很大程度上降低用户 ...
- 制作自己cocoapods库
https://www.cnblogs.com/czc-wjm/p/5958103.html 今天来讲一下cocoapods制作,网上教程很多,就不再讲理论,直接操作: 1.创建仓库: 2.将仓库克隆 ...
- 搭建SpringMVC+Hibernate
1.首先加入相关的jar包,配置web.xml文件,如下: <?xml version="1.0" encoding="UTF-8"?> <w ...
- 【动态规划】 EditDistance
思路参考: https://www.cnblogs.com/littlepanpc/p/7895810.html 代码参考:https://leetcode.com/problems/edit-dis ...
- Linux centos nginx下载安装初步
下载源码包解压编译 1.下载 # wget http://nginx.org/download/nginx-1.9.9.tar.gz 2.解压 # tar xvf nginx-1.9.9.tar.gz ...
- gitlab搭建和使用
原文地址:https://blog.csdn.net/zhushuai662/article/details/79581377 大家常听说Git.Github.Gitlab,很多人对着三个词很懵逼,分 ...
- 重写URL
更新网页url /***省略部分代码***/ function rewriteUrl(url) { if (url.substr(0, 1) === "/") { url = (r ...
- Hbuilder工具使用
现在用的版本是:HBuilder 9.1.19.201808300739 前段时间自动更新了下,也忘记了是更新后js代码不能正常代码提示,还是又发生了什么事情,导致了不能正常提示,也没时间去排查,卸载 ...