使用数组改造解析 json 对象
Posted
技术标签:
【中文标题】使用数组改造解析 json 对象【英文标题】:parsing json object with array retrofit 【发布时间】:2017-05-12 14:15:16 【问题描述】:我正在尝试使用改造 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": []
]
我的模型课放在这里。
电视频道
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()));
);
提前致谢。
【问题讨论】:
尝试在模型本身中使用 ArrayList 使用此站点将您的JSOn
转换为POGO
`jsonschema2pojo.org
我已经解决了这个问题。我如何检索字段 data 为空,这导致了麻烦。谢谢大家的帮助!!!
您是如何解决问题的?我无法检索数据...
【参考方案1】:
您不需要关注....只需拨打tvChannel.getData()
ArrayList<Data> datas = new ArrayList<Data>(Arrays.asList(response.body().getData()));
【讨论】:
【参考方案2】:像这样用 List 替换数组:
@SerializedName("data")
private List<Data> data;
public List<Data> getData()
return data;
public void setData(List<Data> data)
this.data = data;
【讨论】:
【参考方案3】:试试这个
1) 模型类的变化
Data[] data
到 ArrayList<Data>
2) 像
一样获取“数据”List<Data> datas=tvChannel.getData();
我希望它有效。
【讨论】:
【参考方案4】:问题出在你的 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;
【讨论】:
【参考方案5】:尝试使用自定义数据类而不是使用数据导入使您的模型复杂化(顺便问一下,您导入了什么数据类?):
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;
【讨论】:
【参考方案6】:试试这个,希望对你有帮助。
private String status;
private String message;
@SerializedName("data")
@Expose() <-- this is important
private List<Data> data;
........
public List<Data> getData()
return data;
public void setData(List<Data> data)
this.data = data;
【讨论】:
以上是关于使用数组改造解析 json 对象的主要内容,如果未能解决你的问题,请参考以下文章