gpt4 book ai didi

android - 使用数组改造解析 json 对象

转载 作者:太空狗 更新时间:2023-10-29 14:45:13 25 4
gpt4 key购买 nike

我正在尝试使用改造 2.1.0 解析下一个 json 对象,但我不知道为什么 data 字段中没有任何数据,它始终为空。

{
"status": "OK",
"message": "Upload succesfull",
"data": [
{
"channel": "madrid",
"date": "20161212221",
"metadata": {
"id": "tvmadrid",
"channel_id": "tvmadrid.com",
"channel_name": "mad",
},
"promos": []
},
{
"channel": "barca",
"date": "20161233232",
"metadata": {
"id": "tvbarca",
"channel_id": "tvbarca.com",
"channel_name": "barca",
},
"promos": []
}
]
}

我的模型类在这里。

TvChannel

public class TvChannel {
private String status;
private String message;
private Data[] data;

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}



public Data[] getData() {
return data;
}

public void setData(Data[] data) {
this.data = data;
}


@Override
public String toString() {
return "status " + status + "\n"
+ "message " + message + "\n"
+ "Data " + data.toString();
}

这是一个调用API的方法

public void getTvChannel(User user, MultipartBody.Part body) {

//user.getData().getAutenticate(),

Call<TvChannel> callTvChannel = getAPI().listTvChannel(user.getData().getAutenticate(), body);
callTvChannel.enqueue(new Callback<TvChannel>() {
@Override
public void onResponse(Call<TvChannel> call, Response<TvChannel> response) {
if (response.isSuccessful()) {




Log.d(TAG, "body to string: " + response.body().getStatus());

TvChannel tvChannel = response.body();



ArrayList<Data> datas = new ArrayList<Data>(Arrays.asList(response.body().getData()));

Log.d(TAG + " getTvChannel ", " onResponse: status: " + tvChannel.getStatus() + " message: " + tvChannel.getMessage() );

for (Data data : datas) {
Log.d(TAG,data.toString());
}

} else {
Log.d(TAG + " getTvChannel", " onResponse " + "Error Code " + response.code());
}
}

@Override
public void onFailure(Call<TvChannel> call, Throwable t) {
Log.d(TAG + " getTvChannel ", " onFailure " + " Didn't work " + t.getMessage() + " " + t.getCause() + " \n" + Arrays.toString(t.getStackTrace()));


}
});
}

提前致谢。

最佳答案

您不需要关注....只需调用 tvChannel.getData()

ArrayList<Data> datas = new ArrayList<Data>(Arrays.asList(response.body().getData()));

关于android - 使用数组改造解析 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41359629/

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