反序列化此API响应的最佳方法是什么?

时间:2017-10-27 16:50:19

标签: java json deserialization web-api-testing

在反序列化API响应时,在保存新课程时,我返回以下内容:

{
    "Error": false,
    "Message": "Course was saved successfully",
    "Result": "1",
    "DataReturn": null
}

我将其反序列化为以下GeneralResultModel.class

package models;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import models.educationapi.GetCourseDataReturn;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"Error",
"Message",
"Result",
"DataReturn"
})
public class GeneralResultModel implements Serializable
{

@JsonProperty("Error")
private Boolean error;
@JsonProperty("Message")
private String message;
@JsonProperty("Result")
private Object result;
@JsonProperty("DataReturn")
private GetCourseDataReturn dataReturn;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
private final static long serialVersionUID = 4063931322348612559L;

/**
* No args constructor for use in serialization
* 
*/
public GeneralResultModel() {
      }

      /**
      * 
      * @param message
      * @param dataReturn
      * @param result
      * @param error
      */
      public GeneralResultModel(Boolean error, String message, Object result, GetCourseDataReturn dataReturn) {
      super();
      this.error = error;
      this.message = message;
      this.result = result;
      this.dataReturn = dataReturn;
      }

      @JsonProperty("Error")
      public Boolean getError() {
      return error;
      }

      @JsonProperty("Error")
      public void setError(Boolean error) {
      this.error = error;
      }

      @JsonProperty("Message")
      public String getMessage() {
      return message;
      }

      @JsonProperty("Message")
      public void setMessage(String message) {
      this.message = message;
      }

      @JsonProperty("Result")
      public Object getResult() {
      return result;
      }

      @JsonProperty("Result")
      public void setResult(Object result) {
      this.result = result;
      }

      @JsonProperty("DataReturn")
      public Object getDataReturn() {
      return dataReturn;
      }

      @JsonProperty("DataReturn")
      public void setDataReturn(GetCourseDataReturn dataReturn) {
      this.dataReturn = dataReturn;
      }

      @JsonAnyGetter
      public Map<String, Object> getAdditionalProperties() {
      return this.additionalProperties;
      }

      @JsonAnySetter
      public void setAdditionalProperty(String name, Object value) {
      this.additionalProperties.put(name, value);
      }



      @Override
    public String toString() {
        return "[error=" + error + ", message=" + message + ", result=" + result + ", dataReturn="
                + dataReturn +"]";
    }

    @Override
      public boolean equals(Object toCompare) {        

        if (toCompare == this) {
          return false;
        }

        if ((toCompare instanceof GeneralResultModel) == false) {
          return false;
        }

        GeneralResultModel tc = (GeneralResultModel) toCompare;

        if (tc.getResult() == null) {
            tc.setResult("null");
        }

        if (this.getResult() == null) {
            this.setResult("null");
        }

        if (tc.getError() == this.getError() && tc.getDataReturn() == this.getDataReturn() && tc.getMessage().equals(this.getMessage())
            && tc.getResult().equals(this.getResult())) {
              return true;
            }   

        return false;
      }

}

当成功使用/ getCourse时,我检索到相同的类似响应,只有这次DataReturn不为null,它填充了 inner?类型为Course的对象

如何对此类内容进行强大的反序列化,如果可能的话,如何在这两种情况下使用相同的GeneralResultModel?

{
    "Error": false,
    "Message": "",
    "Result": null,
    "DataReturn": [
        {
            "Id": 2,
            "Name": "stack",
            "Description": "overflow#1",
            "ThumbnailFileId": 1,
            "LocationId": 1,


    "PersonId": 0,
        "ProgressPercentage": 0
    },
    {
        "Id": 3,
        "Name": "stack#2",
        "Description": "overflow#2",
        "ThumbnailFileId": 1,
        "LocationId": 1,
        "PersonId": 0,
        "ProgressPercentage": 0
    },
    {
        "Id": 4,
        "Name": "stack#3",
        "Description": "overflow#3",
        "ThumbnailFileId": 1,
        "LocationId": 1,
        "PersonId": 0,
        "ProgressPercentage": 0
    }
]
}

这个确切的响应对于获得A,获得B,获得C是相同的,除了List会有所不同,所以我如何根据我收到的对象类型来迎合这个?

1 个答案:

答案 0 :(得分:0)

关注Google的Gson。

你所要做的就是

Gson gson = new Gson();
GeneralResultModel generalResultModel = gson.fromJson(json, GeneralResultModel.class)

https://github.com/google/gson