我正在尝试使用改装2.1.0解析下一个json对象,但我不知道为什么字段数据中没有任何数据它始终为空。
{
"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()));
}
});
}
提前感谢。
答案 0 :(得分:0)
您不需要关注....只需致电tvChannel.getData()
ArrayList<Data> datas = new ArrayList<Data>(Arrays.asList(response.body().getData()));
答案 1 :(得分:0)
将数组替换为List,如下所示:
@SerializedName("data")
private List<Data> data;
public List<Data> getData() {
return data;
}
public void setData(List<Data> data) {
this.data = data;
}
答案 2 :(得分:0)
试试这个
1)模型类的变化
Data[] data
至ArrayList<Data>
2)获取“数据”,如
List<Data> datas=tvChannel.getData();
我希望它有效。
答案 3 :(得分:0)
问题出在你的json, 请把json放到jsonlint.com
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Datum {
@SerializedName("channel")
@Expose
private String channel;
@SerializedName("date")
@Expose
private String date;
@SerializedName("metadata")
@Expose
private Metadata metadata;
@SerializedName("promos")
@Expose
private List<Object> promos = null;
public String getChannel() {
return channel;
}
public void setChannel(String channel) {
this.channel = channel;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public Metadata getMetadata() {
return metadata;
}
public void setMetadata(Metadata metadata) {
this.metadata = metadata;
}
public List<Object> getPromos() {
return promos;
}
public void setPromos(List<Object> promos) {
this.promos = promos;
}
}
-----------------------------------com.example.Example.java-----------------------------------
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
@SerializedName("status")
@Expose
private String status;
@SerializedName("message")
@Expose
private String message;
@SerializedName("data")
@Expose
private List<Datum> data = null;
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 List<Datum> getData() {
return data;
}
public void setData(List<Datum> data) {
this.data = data;
}
}
-----------------------------------com.example.Metadata.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Metadata {
@SerializedName("id")
@Expose
private String id;
@SerializedName("channel_id")
@Expose
private String channelId;
@SerializedName("channel_name")
@Expose
private String channelName;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getChannelId() {
return channelId;
}
public void setChannelId(String channelId) {
this.channelId = channelId;
}
public String getChannelName() {
return channelName;
}
public void setChannelName(String channelName) {
this.channelName = channelName;
}
}
答案 4 :(得分:0)
尝试使用自定义数据类复杂化您的模型,而不是使用数据导入(顺便说一下,您导入了哪些数据类?):
public class TvChannel {
private String status;
private String message;
ArrayList<Data> data;
...
}
public class Data
{
String channel;
int date;
Metadata metadata;
String[] promos;//of course, if that's string array
}
public class Metadata
{
String id;
String channel_id;
String channel_name;
}
你可以得到这样的数据:
TvChannel tvChannel = response.body();
String metadataId = tvChannel.data.get(index).metadata.id;
答案 5 :(得分:0)
尝试一下,希望对您有所帮助。
import Vue from 'vue'
import Vuex from 'vuex'
import * as user from './modules/user.js'
import * as auth from './modules/auth.js'
import * as group from './modules/group'
import * as system from './modules/system'
import * as module from './modules/module'