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 ...
随机推荐
- MD5密码加密
using System; using System.Security.Cryptography; using System.Text; namespace DimoNetwork.Common.DE ...
- 【转】Java学习---集合框架那些事
[原文]https://www.toutiao.com/i6593220692525711885/ Arraylist 与 LinkedList 异同 1. 是否保证线程安全: ArrayList 和 ...
- 【转】Ubuntu做日常开发电脑的系统是一种怎样的体验
[原文]https://www.toutiao.com/i6594291159911105031/ Ubuntu 我现在已经基本不开windows了.学习娱乐开发基本都在Ubuntu 首先你要接受的是 ...
- 【转】Linux 高级的视角来查看Linux引导过程
[原文]https://www.toutiao.com/i6594210975480545800/ 1.概述 图 1 是我们在20,000 英尺的高度看到的视图. 当系统首次引导时,或系统被重置时,处 ...
- Qt: QSqlRecord字段值为null时注意事项
QSqlRecord在对应字段值为null时,QSqlRecord::value返回的QVariant是有效但为null(相当于使用QVariant(Type type)构造的),所以此时做对应类型的 ...
- 【Android自动化】Subprocess.check_output()简单用法
# -*- coding:utf-8 -*- import os import sys import subprocess from uiautomator import device as d cm ...
- [Python] 启动 uiautomatorviewer2之后,连接成功后重新 reload画面时提示 ('Connection aborted.', error(10054, ''))
[问题] 出现该问题不管是重启手机还是启动手机里面 uiautomator的服务,都无济于事,只有通过下面方法进行重新初使化方能解决问题 [解决方法] 在命令窗口执行如下命令 python -m ui ...
- 三、git管理修改
一.修改提交 如下图,Git分工作区和版本库(.git隐藏目录中). 在每次修改后 git add "file name" 其实是把修改内容提交到本地版本库的 暂存区(stage) ...
- BZOJ3173:[TJOI2013]最长上升子序列(Splay)
Description 给定一个序列,初始为空.现在我们将1到N的数字插入到序列中,每次将一个数字插入到一个特定的位置.每插入一个数字,我们都想知道此时最长上升子序列长度是多少? Input 第一行一 ...
- jupyter中添加conda环境
安装完Anaconda利用conda创建了虚拟环境,但是启动jupyter notebook之后却找不到虚拟环境. 实际上是由于在虚拟环境下缺少kernel.json文件,解决方法如下: 首先安装ip ...