Json和Map互转,四个包(org.json/net.sf.json/com.google.gson/com.alibaba.fastjson)
目前使用的(org.json/net.sf.json/com.google.gson/com.alibaba.fastjson)这四种json-map互转,其他的以后在补充。。。。。。。。。。。。。。
导入的jar有:
commons-beanutils-1.6.1.jar
commons-lang-2.1.jar
ezmorph-1.0.3.jar
jackson-all-1.8.5.jar
gson-2.2.4.jar
json-lib-2.2.2-jdk15.jar
json.jar
fastjson-1.1.32.jar
/**
*
*/
package map2json; import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry; import net.sf.json.JSONArray; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson; /**
* @author hy
* @date 2019-02-25 15:45:35
*
*/
public class map2json { public static void main(String[] args) {
map2jsonstr1();
map2jsonstr2();
map2jsonstr3();
map2jsonstr4();
} // net.sf.json包
public static void map2jsonstr1() {
Map<String, Object> map = new LinkedHashMap<String, Object>();
map.put("1", "a");
map.put("2", "b");
map.put("3", "c");
map.put("4", "d");
map.put("5", "e");
map.put("6", new String[] { "aa", "bb" });
// 多个不同包的同名类,需要指明指哪个包里的
net.sf.json.JSONObject jo = net.sf.json.JSONObject.fromObject(map);
System.out.println(jo.toString());
// 数组
JSONArray json = JSONArray.fromObject(map);
System.out.println(json.toString());
// 将json数据再转回map
net.sf.json.JSONObject myJson = net.sf.json.JSONObject.fromObject(map);
@SuppressWarnings("unchecked")
Map<Object, Object> m = myJson;
for (Entry<Object, Object> entry : m.entrySet()) {
System.out.println(entry.getKey().toString() + ":"
+ entry.getValue().toString());
}
} // org.json包
public static void map2jsonstr2() {
Map<String, String> map = new LinkedHashMap<String, String>();
map.put("1", "a");
map.put("2", "b");
map.put("3", "c");
map.put("4", "d");
map.put("5", "e");
// org.json
org.json.JSONObject js = new org.json.JSONObject(map);
System.out.println(js.toString());
Map<Object, Object> ma = new HashMap<>();
@SuppressWarnings("rawtypes")
Iterator it = js.keys();
while (it.hasNext()) {
String key = (String) it.next();
// 得到value的值
Object value = js.get(key);
// System.out.println(key+":"+valStr);
ma.put(key, value.toString()); }
for (Entry<Object, Object> mp : ma.entrySet()) {
System.out.println(mp.getKey() + ":" + mp.getValue());
}
} // com.google.gson包
public static void map2jsonstr3() { Map<String, String> map = new LinkedHashMap<String, String>();
map.put("1", "a");
map.put("2", "b");
map.put("3", "c");
map.put("4", "d");
map.put("5", "e"); Gson gson = new Gson();
String jsonStr = gson.toJson(map);
System.out.println(jsonStr); Map<Object, Object> ma = new HashMap<>();
ma = gson.fromJson(jsonStr, Map.class); for (Entry<Object, Object> mp : ma.entrySet()) {
System.out.println(mp.getKey() + ":" + mp.getValue());
} } // com.alibaba.fastjson包
public static void map2jsonstr4() { Map<String, String> map = new LinkedHashMap<String, String>();
map.put("1", "a");
map.put("2", "b");
map.put("3", "c");
map.put("4", "d");
map.put("5", "e");
// map转json
com.alibaba.fastjson.JSONObject jsonObject = (JSONObject) com.alibaba.fastjson.JSONObject
.toJSON(map);
String alijson = jsonObject.toJSONString();
System.out.println(alijson);
// json转map
/*
* Map<String, String> maps = (Map<String, String>) JSON.parse(alijson);
* for (Entry<String, String> alima :maps.entrySet()) {
* System.out.println(alima.getKey()+":"+alima.getValue()); }
*/
Map alimap = JSON.parseObject(alijson, Map.class);
for (Object obj : alimap.keySet()) {
System.out.println(obj + ":" + alimap.get(obj));
}
} }
Json和Map互转,四个包(org.json/net.sf.json/com.google.gson/com.alibaba.fastjson)的更多相关文章
- JavaScript Json与Map互转以及Map对象的取值方式
Json格式(Json字符串) : var json='{"name": "lily","age":"15"}' Map ...
- 转:JSON与Map互转
JSON字符串与Map互转 //一.map转为json字符串 public static String map2jsonstr(Map<String,?> map){ return J ...
- json、map互转
首先,json转map 方法一: Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); 或 Gs ...
- 关于maven包的引入net.sf.json的问题
最开始通过在pom.xml文件中加入 <dependency> <groupId>net.sf.json-lib</groupId> <artifactId& ...
- 不同Json工具对空串和NULL的序列号处理:net.sf.json 和 fastjson
目录 1.测试代码 2.测试结果: 3.总结: 4.注:Maven中引入net.sf.json的方式 net.sf.json 和 fastjson 对于空串和NULL的处理: 1.测试代码 packa ...
- [转] golang中struct、json、map互相转化
一.Json和struct互换 (1)Json转struct例子: type People struct { Name string `json:"name_title"` Age ...
- java json与map互相转换(二)
java json与map互相转换(二) CreationTime--2018年7月16日15点09分 Author:Marydon 1.准备工作 所需jar包: commons-beanutil ...
- net.sf.json与fastjson两种jar包的使用
首先说清楚:这两种方式是进行json解析的两种不同的方式而已,哪一种都可以. 一.引入net.sf.json包 首先用net.sf.json包,当然你要导入很多包来支持commons-beanutil ...
- json对象字符串互转
json对象字符串互转 1.Node.js中 JSON.parse(jsonstr); //可以将json字符串转换成json对象 JSON.stringify(jsonobj); //可以将json ...
随机推荐
- [Android疑难杂症]动态改变Background后Padding无效的问题
前言 在Layout中指定好background和padding以后,程序里面动态修改background之后padding就失效了,貌似是一个BUG,这里找到了一篇英文文章,简单翻译分享一下. 声明 ...
- 如何学好PHP
1.明确自己的学习目标和大的方向,选择并锁定一门语言,按照自己的学习方向努力学习.认真研究. 2.学会配置PHP的开发环境,选择一种适合自己的开发工具. 3.基础扎实,多阅读一些基础教材,了解基本的编 ...
- 流Stream个人学习理解
1.Stream类 命名空间:System.IO 程序集:mscorlib 流是对字节序列的抽象,提供字节序列的一般视图. 流的操作包括三个方面: 1.读取(Read):将流数据传入到数据结构 2.写 ...
- happiness[国家集训队2011(吴确)]
[试题来源] 2011中国国家集训队命题答辩 [问题描述] 高一一班的座位表是个n*m的矩阵,经过一个学期的相处,每个同学和前后左右相邻的同学互相成为了好朋友.这学期要分文理科了,每个同学对于选择文科 ...
- 查看apk签名信息
经常在注册开发者的时候会遇到要求填写申请应用的应用签名: 有两种很方便的方法: 1.如果没有源码或者没有打开eclipse,直接下载这个应用应用下载链接 使用截图,只要把包名输入,自动会出现签名信息. ...
- Java读取Excel并与SqlServer库中的数据比较
先说说需求.在SQL server数据库中的表里存在一些数据,现在整理的Excel文档中也存在一些数据,现在需要通过根据比较某个字段值(唯一)来判断出,在库中有但excel中没有的数据. 大概的思路就 ...
- 密码与安全新技术专题之AI与密码
20189217 2018-2019-2 <密码与安全新技术专题>第五周作业 课程:<密码与安全新技术专题> 班级: 1892 姓名: 李熹桥 学号:20189214 上课教师 ...
- JSLinux
JSLinuxhttps://bellard.org/jslinux/vm.html?url=https://bellard.org/jslinux/win2k.cfg&mem=192& ...
- 爬虫_豆瓣电影top250 (正则表达式)
一样的套路,就是多线程还没弄 import requests import re import json headers = 'Mozilla/5.0 (Windows NT 10.0; WOW64) ...
- CentOS6.x下,tomcat - web项目部署
1. 安装tomcat tomcat安装方法:http://www.cnblogs.com/vurtne-lu/p/6478440.html 2. 配置tomcat 修改server.xml文件 &l ...