目前使用的(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)的更多相关文章

  1. JavaScript Json与Map互转以及Map对象的取值方式

    Json格式(Json字符串) : var json='{"name": "lily","age":"15"}' Map ...

  2. 转:JSON与Map互转

    JSON字符串与Map互转   //一.map转为json字符串 public static String map2jsonstr(Map<String,?> map){ return J ...

  3. json、map互转

    首先,json转map 方法一: Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); 或 Gs ...

  4. 关于maven包的引入net.sf.json的问题

    最开始通过在pom.xml文件中加入 <dependency> <groupId>net.sf.json-lib</groupId> <artifactId& ...

  5. 不同Json工具对空串和NULL的序列号处理:net.sf.json 和 fastjson

    目录 1.测试代码 2.测试结果: 3.总结: 4.注:Maven中引入net.sf.json的方式 net.sf.json 和 fastjson 对于空串和NULL的处理: 1.测试代码 packa ...

  6. [转] golang中struct、json、map互相转化

    一.Json和struct互换 (1)Json转struct例子: type People struct { Name string `json:"name_title"` Age ...

  7. java json与map互相转换(二)

      java json与map互相转换(二) CreationTime--2018年7月16日15点09分 Author:Marydon 1.准备工作 所需jar包: commons-beanutil ...

  8. net.sf.json与fastjson两种jar包的使用

    首先说清楚:这两种方式是进行json解析的两种不同的方式而已,哪一种都可以. 一.引入net.sf.json包 首先用net.sf.json包,当然你要导入很多包来支持commons-beanutil ...

  9. json对象字符串互转

    json对象字符串互转 1.Node.js中 JSON.parse(jsonstr); //可以将json字符串转换成json对象 JSON.stringify(jsonobj); //可以将json ...

随机推荐

  1. Windows7系统中怎么Ping端口?利用telnet命令Ping 端口的方法

    telnet www.baidu.com 80 端口打开的情况下,链接成功,则进入Telnet页面(全黑的),证明端口可用.

  2. [Errno 256] No more mirrors to try 解决方法

    安装tree时遇到问题yum [Errno 256] No more mirrors to try 解决方法: 1.yum clean all 2.yum makecache 3.yum update ...

  3. 【转】Java学习---快速掌握RPC原理及实现

    [原文]https://www.toutiao.com/i6592365493435236872/ ​RPC概述 RPC(Remote Procedure Call)即远程过程调用,也就是说两台服务器 ...

  4. 阿里云朱照远: AI打开新视界 8K时代已来!

    2018年4月11-12日,2018亚太CDN峰会在北京隆重召开,大会由亚太CDN领袖论坛.电视云论坛.短视频论坛.视频云论坛.新技术论坛.运营商论坛.国际云论坛等7大部分组成.在亚太CDN领袖峰会上 ...

  5. MySQL基础之 如何删除主键

    我们在一个表中设置了主键之后,那么如何删除主键呢? 删除主键的语法是: ALTER TABLE TABLE_NAME DROP PRIMARY KEY; 在这里我们要考虑两种情况: 1.可以直接使用d ...

  6. 第 15 章 位操作(invert4)

    /*------------------------------------ invert4.c -- 使用位操作显示二进制 ------------------------------------* ...

  7. BZOJ 1113 海报 单调栈

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1113 题目大意: N个矩形,排成一排. 现在希望用尽量少的矩形海报Cover住它们. ...

  8. MySQL多实例.md

    MySQL5.7多实例配置 数据库实例1配置文件 # cat /etc/my.cnf [mysqld] datadir=/data/mysql port=3306 socket=/tmp/mysql. ...

  9. Azkaban学习之路 (二)Azkaban的安装

    安装过程 1.软件介绍 Azkaban Web 服务器:azkaban-web-server-2.5.0.tar.gz Azkaban Excutor 执行服务器:azkaban-executor-s ...

  10. ethjs-1-了解

    https://github.com/ethjs/ethjs/blob/master/docs/user-guide.md Install npm install --save ethjs Usage ...