FastJson 泛型的反序列化

前端之家收集整理的这篇文章主要介绍了FastJson 泛型的反序列化前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1. 问题

服务端返回的json数据格式大致就是这样,但是data可能是object也有可能是list 或者hashmap ,如果是这样该怎么写呢,用FastJson

/**
 * Created by Albert.Liu on 15/10/7.
 */
public class BaseResponseWS<T> implements IResponse {
    /**
     * 0为成功,其他为错误
     */
    private int error;
    private String msg;
    private String canfee;
    private String onefee;
    private String usercarid;
    private T data;


    public int getError() {
        return error;
    }

    public void setError(int error) {
        this.error = error;
    }

    public boolean isSuccess() {
        return error == 0;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getCanfee() {
        return canfee;
    }

    public void setCanfee(String canfee) {
        this.canfee = canfee;
    }

    public String getOnefee() {
        return onefee;
    }

    public void setOnefee(String onefee) {
        this.onefee = onefee;
    }

    public String getUsercarid() {
        return usercarid;
    }

    public void setUsercarid(String usercarid) {
        this.usercarid = usercarid;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}

2. 解决方

BaseResponseWS<List<ViolationWS>> responseWS = JSON.parSEObject(response,new TypeReference<BaseResponseWS<List<ViolationWS>>>() {});
原文链接:https://www.f2er.com/json/289592.html

猜你在找的Json相关文章