第一种alibaba falstjson:
Map转JSON
Map map = new HashMap(); map.put("a", "a"); map.put("b", "123"); JSONObject json = new JSONObject(map);
map转string
Map map = new HashMap<>(); map.put("a", "b"); String s = JSONObject.toJSONString(map);
JSON转String
JSONObject json = new JSONObject(); json.put("c", "v"); json.put("z", "123n); json.toJSONString();
JSON转Map
JSONObject json = new JSONObject(); json.put("ccc", "321"); json.put("bbb", "123"); Map map = (Map)json;
String转JSON
String str = "{"username":"dsad","qwewqe":"123"}";JSONObject json = JSONObject.parseObject(str);
2 google
maven坐标
com.google.code.gson gson 2.3.1
//Map转换成JSON
Map map = new HashMap(); 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 entry = (Map.Entry)mapData;System.out.println(entry.getKey()+"--->"+entry.getValue());}
map中含有对象Map -> JSON
//Map -> JSONMap map = new HashMap(); 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);
//JSON -> Map
Map map1 = (Map)JSON.parse(json); for (String key : map1.keySet()) { System.out.println(key+":"+map1.get(key)); }
-------------------=-------------------------附–MAP的ASCII排序-----------------------=------------------------
StringBuilder sb = new StringBuilder(); List keys = new ArrayList(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");这个就不用看了~~~
来源地址:https://blog.csdn.net/jcc4261/article/details/129660911