关于数据序列化(3),JSON的方式,FastJson序列化对象和List集合示例

JSON也是对象序列化的一种方式

1. 示例

public class FriendLikeVO {
    public String sn; 
    public int score; 
    public long time;

    public FriendLikeVO(JSONObject jo) {
        this.sn = jo.getString("sn");
        this.score = jo.getIntValue("score");
        this.time = jo.getLong("time");
    }

    public FriendLikeVO() {
    }

    public FriendLikeVO(String sn, int score, long time) {
        this.sn = sn;
        this.score = score;
        this.time = time;
    }

    /**
     * 把Json转换为List
     */
    public static List<FriendLikeVO> jsonToList(String json) {
        List<FriendLikeVO> result = new ArrayList<FriendLikeVO>();

        JSONArray ja = JSON.parseArray(json);
        if (ja == null) {
            return result;
        }
        for (int i = 0; i < ja.size(); i++) {
            FriendLikeVO vo = new FriendLikeVO(ja.getJSONObject(i));
            result.add(vo);
        }
        return result;
    }

    /**
     * 将List转换为Json
     */
    public static String listToJson(List<FriendLikeVO> likeList) {
        JSONArray ja = new JSONArray();

        likeList.stream().forEach(vo -> {
            JSONObject jo = new JSONObject();
            jo.put("sn", vo.sn);
            jo.put("score", vo.score);
            jo.put("time", vo.time);
            ja.add(jo);
        });
        return ja.toJSONString();
    }
}
  • 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
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

2. 不想序列化的地方用ransient标注

但是用fastjson的序列化更简单一些,如果不想序列化的字段就标注transient就可以了

public class FriendLike {
    public String sn; // 好友ID,目前为IP
    public int score; // 点赞的点数
    public long time;
        public transient int cc;
    public FriendLike() {
    }

    public FriendLike(String sn, int score, long time) {
        this.sn = sn;
        this.score = score;
        this.time = time;
    }

    /**
     * 把Json转换为List
     */
    @SuppressWarnings("unchecked")
    public static List<FriendLike> jsonToList(String json) {
        if(Util.isEmpty(json)){
            return new ArrayList<FriendLike>();
        }
        return JSON.parseObject(json, List.class);

    }
    /**
     * 将List转换为Json
     */
    public static String listToJson(List<FriendLike> likeList) {
        if(null == likeList){
            return null;
        }
        return JSON.toJSONString(likeList);
    }
}
  • 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
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

3.循环引用 不建议使用fastjson有bug

fastjson的循环引用问题,反序列化也是可以成功的 
测试代码

FriendLike fk = new FriendLike("a", 2, System.currentTimeMillis());
FriendLike fk2 = new FriendLike("b", 2, System.currentTimeMillis());
List<FriendLike> list = new ArrayList<>();
list.add(fk);
list.add(fk2);
list.add(fk2);
String a = JSON.toJSONString(list);
List<FriendLike> list2 = JSON.parseObject(a, List.class);
System.out.println(a);
System.out.println(list);
输出结果
[{"score":2,"sn":"a","time":1470211141179},{"score":2,"sn":"b","time":1470211141179},{"$ref":"$[1]"}]
[FriendLike[sn=a,score=2,time=1470211141179], FriendLike[sn=b,score=2,time=1470211141179], FriendLike[sn=b,score=2,time=1470211141179]]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

4. 序列化List

注意一个坑,如果要序列化List的时候应该使用JSON.parseArray这个方法,并且注意,被序列化的对象类型要有无参构造方法!

    @SuppressWarnings("unchecked")
    public static List<FriendHelpVO> jsonToList(String json) {
        if (Util.isEmpty(json)) {
            return new ArrayList<FriendHelpVO>();
        }
        return JSON.parseArray(json, FriendHelpVO.class);

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

5. 反序列化

tips: 
FastJson反序列化的原理是 如果

JSON.parseObject(json, obj.class);
  • 1

如果要反序列的话的对象中有集合属性或者要反序列化一个集合,fastJson的反序列化,只能根据传入的class来生成一个集合,但是集合的元素为JSONObject。 
如果是对象中的某个属性的声明不是具体的类。也会造成这种情况。 
如果想完全的用fastjson序列化反序列化一个对象。 
那么生成json字符串的时候需要 
这样

JSON.toJSONString(v2,SerializerFeature.WriteClassName);

猜你喜欢

转载自blog.csdn.net/chang_ge/article/details/82588544