简体   繁体   中英

Get nested JSON object with Jackson using retrofit

I have api with all request like:

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

For example:

{
  "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": []
}

Also if success==true is possible call onResponse block and if success==false call onFailure block with error text from errors variable from json. Api contains different response data models for data block in json response and i need that data model class would be dynamically changed. Is there the way to create something like this? All responses will be useful for me.

Try this

Add this compile 'com.google.code.gson:gson:2.6.2' dependencies in your gradle file.

After getting your response jsonObject Pass like this

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();
        }
    }
}

Create Pojo class 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;
    }
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM