这篇文章主要为大家详细介绍了Java语言实现map转换成json的几种方法,文中的示例代码讲解详细,对我们学习Java有一定帮助,需要的可以参考一下
1.alibaba falstjson
1.Map转JSON
Map<String, Object> map = new HashMap<String, Object>();
map.put("a", "a");
map.put("b", "123");
JSONObject json = new JSONObject(map);
2.map转string
Map<String, Object> map = new HashMap<>();
map.put("a", "b");
String s = JSONObject.toJSONString(map);
3.JSON转String
JSONObject json = new JSONObject();
json.put("c", "v");
json.put("z", "123n);
json.toJSONString();
4.JSON转Map
JSONObject json = new JSONObject();
json.put("ccc", "321");
json.put("bbb", "123");
Map<String, Object> map = (Map<String, Object>)json;
5.String转JSON
String str = "{"username":"dsad","qwewqe":"123"}";
JSONObject json = JSONObject.parseObject(str);
2.google
maven坐标
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>
Map转换成JSON
Map<String,String> map = new HashMap<String,String>();
map.put("a","aaa");
map.put("b","bbb");
map.put("c","ccc");
String json=JSON.toJSONString(map);
System.out.println(json);//输出{"a":"aaa","b":"bbb","c":"ccc"}
JSON转换成Map
Map map1 = JSON.parseObject(json);
System.out.println(map1.get("a"));
for (Object mapData : map.entrySet()) {
Map.Entry<String,String> entry = (Map.Entry<String,String>)mapData;
System.out.println(entry.getKey()+"--->"+entry.getValue());
}
/*输出
b--->bbb
c--->ccc
a--->aaa
*/
map中含有对象Map -> JSON
//Map -> JSON
Map<String,Bar> map = new HashMap<String, Bar>();
map.put("a",new Bar());
map.put("b",new Bar());
map.put("c",new Bar());
String json = JSON.toJSONString(map,true);
System.out.println(json);
/*
输出{
"a":{
"barAge":383687382,
"barDate":1494945882018,
"barName":"name_1689176802"
},
"b":{
"barAge":-100528778,
"barDate":1494945882018,
"barName":"name_-878176366"
},
"c":{
"barAge":-344075192,
"barDate":1494945882018,
"barName":"name_-1710740534"
}
}
*/
JSON -> Map
Map<String,Bar> map1 = (Map<String,Bar>)JSON.parse(json);
for (String key : map1.keySet()) {
System.out.println(key+":"+map1.get(key));
}
/*输出
b:{"barAge":-100528778,"barDate":1494945882018,"barName":"name_-878176366"}
c:{"barAge":-344075192,"barDate":1494945882018,"barName":"name_-1710740534"}
a:{"barAge":383687382,"barDate":1494945882018,"barName":"name_1689176802"}
*/
附–MAP的ASCII排序
StringBuilder sb = new StringBuilder();
List<String> keys = new ArrayList<String>(map.keySet());
Collections.sort(keys);//排序。
for(String k : keys){
String v = params.get(k);
if(StringUtils.isNotEmpty(v)){
sb.append(v);
}
}
//return MD5.toMD5(sb+key, "UTF-8");这个就不用看了~~~
到此这篇关于Java实现map转换成json的方法详解的文章就介绍到这了,更多相关Java map转json内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
沃梦达教程
本文标题为:Java实现map转换成json的方法详解
猜你喜欢
- JSP页面间传值问题实例简析 2023-08-03
- ExecutorService Callable Future多线程返回结果原理解析 2023-06-01
- JSP 制作验证码的实例详解 2023-07-30
- Java中的日期时间处理及格式化处理 2023-04-18
- Java实现顺序表的操作详解 2023-05-19
- Springboot整合minio实现文件服务的教程详解 2022-12-03
- Spring Security权限想要细化到按钮实现示例 2023-03-07
- 基于Java Agent的premain方式实现方法耗时监控问题 2023-06-17
- SpringBoot使用thymeleaf实现一个前端表格方法详解 2023-06-06
- 深入了解Spring的事务传播机制 2023-06-02