Java 应为BEGIN_对象,但已通过改装创建BEGIN_阵列
我怎么能用改型来解析这个呢?我得到了一个错误 BEGIN\u对象,但它是BEGIN\u数组 Json在下面,它包含一个results数组对象,其中有一个数组对象为null,还有一些信息对象。似乎null数组就是问题所在,如果可能的话,我需要一些帮助来解决这个问题 型号类别:Java 应为BEGIN_对象,但已通过改装创建BEGIN_阵列,java,json,parsing,retrofit,jsonexception,Java,Json,Parsing,Retrofit,Jsonexception,我怎么能用改型来解析这个呢?我得到了一个错误 BEGIN\u对象,但它是BEGIN\u数组 Json在下面,它包含一个results数组对象,其中有一个数组对象为null,还有一些信息对象。似乎null数组就是问题所在,如果可能的话,我需要一些帮助来解决这个问题 型号类别: public class Response { @SerializedName("results") List<IssuesResults> resultList;
public class Response {
@SerializedName("results")
List<IssuesResults> resultList;
public Response(ArrayList<IssuesResults> resultList) {
this.resultList = resultList;
}
public List<IssuesResults> getResultList() {
return resultList;
}
}
公共类响应{
@SerializedName(“结果”)
列表结果列表;
公共响应(ArrayList结果列表){
this.resultList=resultList;
}
公共列表getResultList(){
返回结果列表;
}
}
Api
@GET("promos/")
Call<Response> getPromos(@Query("api_key") String API_KEY,
@Query("format") String format);
@GET(“promos/”)
调用getPromos(@Query(“api_key”)字符串api_key,
@查询(“格式”)字符串格式);
回购
public MutableLiveData<List<IssuesResults>> getPromos() {
callApi.getPromos(API_KEY,"json").enqueue(new Callback<Response>() {
@Override
public void onResponse(Call<Response> call,
retrofit2.Response<Response> response) {
promosMutableData.setValue(response.body().getResultList());
}
@Override
public void onFailure(Call<Response> call, Throwable t) {
Log.d("PROMOS", "onFailure: " + t);
}
});
return promosMutableData;
}
public MutableLiveData getPromos(){
getPromos(API_KEY,“json”).enqueue(newcallback(){
@凌驾
公共void onResponse(调用,
(2.回应){
promotsTableData.setValue(response.body().getResultList());
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.d(“PROMOS”,“onFailure:+t”);
}
});
返回可升级数据;
}
我假设您使用带有默认设置的Gson。您的问题是,您需要一个issueresults
列表,但它包含这个额外的数组,这意味着默认情况下,它只能序列化为一个对象列表,然后作为一个数组和N个映射作为项
对于这类东西,您需要一些自定义处理,我在这里提供了一个选项,即JsonDeserializer
。我只需要很少的更改就可以使事情变得更简单(对于其他方法来说,它们并不都是必需的,但是对于这个解决方案,我使用了这些方法)。首先,不是内联类型列表,而是实现如下类:
@SuppressWarnings("serial")
public static class ListIssuesResults extends ArrayList<IssuesResults> {
// This override is not needed if you do not mind null values in your resulting list
@Override
public boolean add(IssuesResults e) {
if (null != e) {
return super.add(e);
}
return false;
}
}
然后,实际的自定义JsonDeserializer
:
public static class IssuesResultsDeserializer implements JsonDeserializer<IssuesResults> {
// this you need to not to cause recursion and stack overflow
private static final Gson GSON = new Gson();
@Override
public IssuesResults deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context)
throws JsonParseException {
// this is the trick. if it fails to serialize stuff like [] jsut return null
try {
return GSON.fromJson(json, typeOfT);
} catch (Exception ise) {
return null;
}
}
}
这必须在改造客户建筑阶段完成,如下所示:
public static class Response {
@SerializedName("results")
private ListIssuesResults resultList;
// other stuff
}
public static class IssuesResultsDeserializer implements JsonDeserializer<IssuesResults> {
// this you need to not to cause recursion and stack overflow
private static final Gson GSON = new Gson();
@Override
public IssuesResults deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context)
throws JsonParseException {
// this is the trick. if it fails to serialize stuff like [] jsut return null
try {
return GSON.fromJson(json, typeOfT);
} catch (Exception ise) {
return null;
}
}
}
new GsonBuilder().registerTypeAdapter(IssuesResults.class,
new IssuesResultsDeserializer()).create();