Android 使用改型获得Jackson的嵌套JSON对象

Android 使用改型获得Jackson的嵌套JSON对象,android,nested,jackson,retrofit,Android,Nested,Jackson,Retrofit,我有api,所有的请求都是: { "success": "true", "data": [], "errors": [] } 例如: { "success": "true", "data": [ { "id": "666", "name": "Cars", "imgUrl": "/images/mod_catalog_prod/categories

我有api,所有的请求都是:

{
  "success": "true",
  "data": [],
  "errors": []
}
例如:

{
  "success": "true",
  "data": [
             {
                 "id": "666",
                 "name": "Cars",
                 "imgUrl": "/images/mod_catalog_prod/categories/no-image.jpg",
                 "descr": "",
                 "childRubricCount": 20
            },
            {
                 "id": "667",
                 "name": "Buses",
                 "imgUrl": "/images/mod_catalog_prod/categories/no-image.jpg",
                 "descr": "",
                 "childRubricCount": 14
           }
  ],
  "errors": []
}
另外,如果success==true,则可能调用onResponse块,如果success==false,则调用onFailure块,其中包含来自json的errors变量的错误文本。 Api包含json响应中数据块的不同响应数据模型,我需要动态更改数据模型类。
有没有办法创造出这样的东西?所有的回答都对我有用。

试试这个

public void onRequestSuccess(JSONObject jsonObject) {
    ModelItem item = new Gson().fromJson(jsonObject.toString(), ModelItem.class);
    List<ModelItem.DataItem> dataItems;
    List<ModelItem.ErrorItem> errorItems;

    if (item.getSuccess().equalsIgnoreCase("true")) {
        dataItems = item.getData();
        for (int i = 0; i < dataItems.size(); i++) {
            String name = dataItems.get(i).getName();
        }
    } else {
        errorItems = item.getErrors();
        for (int i = 0; i < errorItems.size(); i++) {
            String name = errorItems.get(i).getErrorMsg();
        }
    }
}
将此编译'com.google.code.gson:gson:2.6.2'依赖项添加到gradle文件中

在得到您的回复后,jsonObject会像这样通过

public void onRequestSuccess(JSONObject jsonObject) {
    ModelItem item = new Gson().fromJson(jsonObject.toString(), ModelItem.class);
    List<ModelItem.DataItem> dataItems;
    List<ModelItem.ErrorItem> errorItems;

    if (item.getSuccess().equalsIgnoreCase("true")) {
        dataItems = item.getData();
        for (int i = 0; i < dataItems.size(); i++) {
            String name = dataItems.get(i).getName();
        }
    } else {
        errorItems = item.getErrors();
        for (int i = 0; i < errorItems.size(); i++) {
            String name = errorItems.get(i).getErrorMsg();
        }
    }
}
public void onRequestSuccess(JSONObject JSONObject){
ModelItem item=new Gson().fromJson(jsonObject.toString(),ModelItem.class);
列出数据项;
列出错误项目;
if(item.getSuccess().equalsIgnoreCase(“true”)){
dataItems=item.getData();
对于(int i=0;i
创建Pojo类ModelItem.java

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
import java.util.List;

public class ModelItem {

@SerializedName("success")
@Expose
private String success;
@SerializedName("data")
@Expose
private List<DataItem> data = new ArrayList<DataItem>();
@SerializedName("errors")
@Expose
private List<ErrorItem> errors = new ArrayList<ErrorItem>();

/**
 * @return The success
 */
public String getSuccess() {
    return success;
}

/**
 * @param success The success
 */
public void setSuccess(String success) {
    this.success = success;
}

/**
 * @return The data
 */
public List<DataItem> getData() {
    return data;
}

/**
 * @param data The data
 */
public void setData(List<DataItem> data) {
    this.data = data;
}

/**
 * @return The errors
 */
public List<ErrorItem> getErrors() {
    return errors;
}

/**
 * @param errors The errors
 */
public void setErrors(List<ErrorItem> errors) {
    this.errors = errors;
}

public class DataItem {

    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("imgUrl")
    @Expose
    private String imgUrl;
    @SerializedName("descr")
    @Expose
    private String descr;
    @SerializedName("childRubricCount")
    @Expose
    private Integer childRubricCount;

    /**
     * @return The id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id The id
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * @return The name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name The name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return The imgUrl
     */
    public String getImgUrl() {
        return imgUrl;
    }

    /**
     * @param imgUrl The imgUrl
     */
    public void setImgUrl(String imgUrl) {
        this.imgUrl = imgUrl;
    }

    /**
     * @return The descr
     */
    public String getDescr() {
        return descr;
    }

    /**
     * @param descr The descr
     */
    public void setDescr(String descr) {
        this.descr = descr;
    }

    /**
     * @return The childRubricCount
     */
    public Integer getChildRubricCount() {
        return childRubricCount;
    }

    /**
     * @param childRubricCount The childRubricCount
     */
    public void setChildRubricCount(Integer childRubricCount) {
        this.childRubricCount = childRubricCount;
    }
}

public class ErrorItem {

    @SerializedName("error_code")
    @Expose
    private String errorCode;
    @SerializedName("error_msg")
    @Expose
    private String errorMsg;

    /**
     * @return The errorCode
     */
    public String getErrorCode() {
        return errorCode;
    }

    /**
     * @param errorCode The error_code
     */
    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    /**
     * @return The errorMsg
     */
    public String getErrorMsg() {
        return errorMsg;
    }

    /**
     * @param errorMsg The error_msg
     */
    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }
}
}
import com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
导入java.util.ArrayList;
导入java.util.List;
公共类模型项{
@序列化名称(“成功”)
@暴露
私人字符串成功;
@SerializedName(“数据”)
@暴露
私有列表数据=新的ArrayList();
@SerializedName(“错误”)
@暴露
私有列表错误=新建ArrayList();
/**
*@回报成功
*/
公共字符串getSuccess(){
回归成功;
}
/**
*@param success成功
*/
public void setSuccess(字符串成功){
成功=成功;
}
/**
*@返回数据
*/
公共列表getData(){
返回数据;
}
/**
*@param data将数据
*/
公共无效设置数据(列表数据){
这个数据=数据;
}
/**
*@返回错误
*/
公共列表getErrors(){
返回错误;
}
/**
*@param errors这些错误
*/
公共void setErrors(列表错误){
这个。错误=错误;
}
公共类数据项{
@序列化名称(“id”)
@暴露
私有字符串id;
@序列化名称(“名称”)
@暴露
私有字符串名称;
@序列化名称(“imgUrl”)
@暴露
私有字符串imgUrl;
@序列化名称(“描述”)
@暴露
私有字符串描述;
@SerializedName(“childRubricCount”)
@暴露
私有整数childRubricCount;
/**
*@返回id
*/
公共字符串getId(){
返回id;
}
/**
*@param id这个id
*/
公共无效集合id(字符串id){
this.id=id;
}
/**
*@返回名称
*/
公共字符串getName(){
返回名称;
}
/**
*@param name指定名称
*/
公共void集合名(字符串名){
this.name=名称;
}
/**
*@returntheimgurl
*/
公共字符串getImgUrl(){
返回图像;
}
/**
*@param imgUrl imgUrl
*/
公共void setImgUrl(字符串imgUrl){
this.imgUrl=imgUrl;
}
/**
*@返回描述
*/
公共字符串getDescr(){
返回描述;
}
/**
*@param descr描述描述
*/
公共void setDescr(字符串descr){
this.descr=descr;
}
/**
*@returnthechildrubriccount
*/
公共整数getChildRubricCount(){
返回childRubricCount;
}
/**
*@param childRubricCount childRubricCount
*/
public void setChildRubricCount(整数childRubricCount){
this.childRubricCount=childRubricCount;
}
}
公共类错误项{
@SerializedName(“错误代码”)
@暴露
私有字符串错误码;
@SerializedName(“错误消息”)
@暴露
私有字符串errorMsg;
/**
*@返回错误代码
*/
公共字符串getErrorCode(){
返回错误码;
}
/**
*@param errorCode错误代码
*/
公共无效设置错误代码(字符串错误代码){
this.errorCode=错误代码;
}
/**
*@返回错误消息
*/
公共字符串getErrorMsg(){
返回errorMsg;
}
/**
*@param errorMsg错误\u msg
*/
public void setErrorMsg(字符串errorMsg){
this.errorMsg=errorMsg;
}
}
}

这样形成数据JsonArray,json数据格式显示错误。“数据”:[{“id”:“666”,“name”:“Cars”,“imgUrl”:“/images/mod_catalog\u prod/categories/no image.jpg”,“descr”:“,“childRubricCount”:20},{“id”:“667”,“name”:“bus”,“imgUrl”:“/images/mod_catalog_prod/categories/no image.jpg”,“descr:”“childRubricCount”:14}]您需要为每个案例生成相同类型的响应,并将错误设置为字符串类型。仅为成功类型生成模型。@Jai Rajesh您说得对,我已经更正了。但问题是,从数据json字段处理不同java类模型的数据的正确方法是什么。如果成功==false,则抛出带有错误消息的异常。@Kundan Kumar Roy我这样做了,but我认为有一种方法可以在Reformation/jackson方面实现这一点。例如,这与适用于GSON的问题相同。您是否使用Reformation?您的错误json数组包含空列表。我添加的错误列表类似于“errors”:[{“error\u code”:“200”,“error\u msg”:“Cars”}]您需要在这里更改所需内容,并在模型类creategetter和setter方法中添加关键参数。