目录

Java类序列化与反序列化工具笔记

1 Alibaba Fastjson

1.Map转JSON

1
2
3
4
        Map<String, Object> Map = new HashMap<String, Object>();
        Map.put("a", "a");
        Map.put("b", "123");
        JSONObject json = new JSONObject(Map);

2.Map转string

1
2
3
        Map<String, Object> map = new HashMap<>();
        map.put("a", "b");
        String s = JSONObject.toJSONString(map);

3.JSON转String

1
2
3
4
        JSONObject json = new JSONObject();
        json.put("c", "v");
        json.put("z", "123n);
        json.toJSONString();

4.JSON转Map

1
2
3
4
        JSONObject json = new JSONObject();
        json.put("ccc", "321");
        json.put("bbb", "123");
        Map<String, Object> map = (Map<String, Object>)json;

5.String转JSON

1
2
        String str = "{\"username\":\"dsad\",\"qwewqe\":\"123\"}";
        JSONObject json = JSONObject.parseObject(str);

2 Google gson

maven坐标

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.3.1</version>
</dependency>

Map转换成JSON

1
2
3
4
5
6
        Map<String,String> map = new HashMap<String,String>(); 
        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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
        Map map1 = JSON.parseObject(json);
        System.out.println(map1.get("a"));
        for (Object mapData : map.entrySet()) {
        Map.Entry<String,String> entry = (Map.Entry<String,String>)mapData;
        System.out.println(entry.getKey()+"--->"+entry.getValue());
        }
        /*输出
        b--->bbb
        c--->ccc
        a--->aaa
        */

Map中含有对象Map -> JSON

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
        //Map -> JSON
        Map<String,Bar> map = new HashMap<String, Bar>(); 
        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); 
        /*
        输出{
            "a":{
                "barAge":383687382,
                "barDate":1494945882018,
                "barName":"name_1689176802"
            },
            "b":{
                "barAge":-100528778,
                "barDate":1494945882018,
                "barName":"name_-878176366"
            },
            "c":{
                "barAge":-344075192,
                "barDate":1494945882018,
                "barName":"name_-1710740534"
            }
        }
        */

JSON -> Map

1
2
3
4
5
6
7
8
9
        Map<String,Bar> map1 = (Map<String,Bar>)JSON.parse(json); 
        for (String key : map1.keySet()) { 
            System.out.println(key+":"+map1.get(key)); 
        } 
        /*输出
        b:{"barAge":-100528778,"barDate":1494945882018,"barName":"name_-878176366"}
        c:{"barAge":-344075192,"barDate":1494945882018,"barName":"name_-1710740534"}
        a:{"barAge":383687382,"barDate":1494945882018,"barName":"name_1689176802"}
        */

附Map的ASCII排序

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
        StringBuilder sb = new StringBuilder();
        List<String> keys = new ArrayList<String>(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");这个就不用看了~~~