How to parse JSON data uniformly, use Gson combined with generic classes to be flexible, let you do it once and for all

As an android client developer, it is important to learn the habit of problem solving and summarizing problems. . . Then the data returned by our app interface is usually in the following format:

{"code":"0000","data": {"code":"0002","resultData":null,"resultMsg":"用户Token过期,请重新登入","error":[],"msg":""}

Put the above json data on the online JSON string to Java entity class (JavaBean) website (http://www.atool.org/json2javabean.php) to generate the following Java entity class:

 

public class JsonRootBean {  
  
    private String code;  
    private Data data;  
    private List<String> error;  
    private String msg;  
    public void setCode(String code) {  
         this.code = code;  
     }  
     public String getCode() {  
         return code;  
     }  
  
    public void setData(Data data) {  
         this.data = data;  
     }  
     public Data getData() {  
         return data;  
     }  
  
    public void setError(List<String> error) {  
         this.error = error;  
     }  
     public List<String> getError() {  
         return error;  
     }  
  
    public void setMsg(String msg) {  
         this.msg = msg;  
     }  
     public String getMsg() {  
         return msg;  
     }  
  
}  

 We know that the data format returned by all interfaces is the same. Take the return result above as an example. The returned Json string is only "data": { "code":"0002","resultData":null ,"resultMsg":"User Token expired, please log in again"}, this

 

 

The returned data in "resultData" in data is different, and other data formats are the same, so we use generic Java entity classes here, and we can do all the parsing at one time!

Change the Data member variable in the Java entity class above to:

public class JsonRootBean<T> {  
  
    private Data<T> data;  
  
   public Data<T> getData() {  
        return data;  
    }  
  public void setData(Data<T> data) {  
        this.data = data;  
    }  
  
}  

 

 

And the Data class is:

public class Data<T> {  
  
    private String code;  
    private T resultData;  
    private String resultMsg;  
  
    public String getCode() {  
        return code;  
    }  
  
    public void setCode(String code) {  
        this.code = code;  
    }  
  
    public T getResultData() {  
        return resultData;  
    }  
  
    public void setResultData(T resultData) {  
        this.resultData = resultData;  
    }  
  
    public String getResultMsg() {  
        return resultMsg;  
    }  
  
    public void setResultMsg(String resultMsg) {  
        this.resultMsg = resultMsg;  
    }  
}  

 Then when we parse the returned Json string, do the following processing:

 

1. If the returned string resultData is directly {"object"}, the processing is as follows:
  Type
 jsonType = new TypeToken<JsonRootBean<TripResultData>>() {}.getType();
JsonRootBean<TripResultData> rootBean = new Gson(). fromJson(jsonStr, jsonType);
Then according to the get method of rootBean, obtain the corresponding data value in turn, and then obtain the specific data according to the specific entity class whose generic type T is TripResultData;
2. If the returned string resultData is directly an
array [ {"object"}], processed as follows:

Type  jsonType = new TypeToken<JsonRootBean<TripResultData>>() {}.getType();

JsonRootBean<List<TripResultData>> rootBean = new Gson().fromJson(jsonStr, jsonType); In fact, the generic entity class is written in the list List<T>;

In fact, at this time, all the data has been parsed into this rootBean, and you can get it according to the method you wrote.

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326127468&siteId=291194637