gpt4 book ai didi

android - 使用改造获取 Jackson 的嵌套 JSON 对象

转载 作者:行者123 更新时间:2023-11-29 20:02:04 25 4
gpt4 key购买 nike

我有所有请求的 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 block ,如果 success==false 调用 onFailure block ,错误文本来自 json 的错误变量。Api 在 json 响应中包含数据 block 的不同响应数据模型,我需要动态更改数据模型类。有没有办法创造这样的东西?所有回复都对我有用。

最佳答案

试试这个

在您的 gradle 文件中添加此 compile 'com.google.code.gson:gson:2.6.2' 依赖项。

像这样得到你的响应jsonObject Pass之后

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

创建 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;
}
}
}

关于android - 使用改造获取 Jackson 的嵌套 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36128557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com