目前使用的(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. vs 2012/2013 等工具中,使用正则表达式,查找、替换

    有这样一个需求,就是一个文本中,需要找出指定格式的字符串进行指定的替换,当前我的真实需求是,一个sql创建触发器的文本,我需要将所有的 包含 TB_SYS 的表名后面添加一个 “_NEW”字符串! 例 ...

  2. JS获取元素宽高的两种情况

    JS获取元素宽高分两种情况, 一.内联样式,也就是直接把width和height写在HTML元素中的style里: 这种情况使用     document.getElementById('xxx'). ...

  3. WebBrowser实现:自动填充网页上的用户名和密码并点击登录按钮

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { ...

  4. PFX文件提取公钥私钥

    jks是JAVA的keytools证书工具支持的证书私钥格式.pfx是微软支持的私钥格式. cer是证书的公钥. 如果是你私人要备份证书的话记得一定要备份成jks或者pfx格式,否则恢复不了. 简单来 ...

  5. Qt: QSqlRecord字段值为null时注意事项

    QSqlRecord在对应字段值为null时,QSqlRecord::value返回的QVariant是有效但为null(相当于使用QVariant(Type type)构造的),所以此时做对应类型的 ...

  6. PyQt5--QLineEdit

    # -*- coding:utf-8 -*- ''' Created on Sep 20, 2018 @author: SaShuangYiBing Comment: ''' import sys f ...

  7. PyQt5--GridLayout

    # -*- coding:utf-8 -*- ''' Created on Sep 13, 2018 @author: SaShuangYiBing ''' import sys from PyQt5 ...

  8. MySQL Error Code文档手册---摘自MySQL官方网站

    This chapter lists the errors that may appear when you call MySQL from any host language. The first ...

  9. CF838D Airplane Arrangements

    传送门:https://www.luogu.org/problemnew/show/CF838D 这道题反正我自己想是毫无头绪,最后还是听了肖大佬的做法. 因为题中说乘客可以从前后门进来,所以我们可以 ...

  10. 微信jsapi退款操作

    引自网络“ 前期准备:当然是搞定了微信支付,不然怎么退款,这次还是使用官方的demo.当然网上可能也有很多大神自己重写和封装了demo,或许更加好用简洁,但是我还是不提倡用,原因如下:(1)可能功能不 ...