我是Retrofit和RX java的新手,我正在尝试解析以下JSON:
{
"result":
{
"offset": 0,
"limit": 2,
"count": 408,
"sort": "",
"results":[
{
"_id": "1",
"iid": "338",
"sv": "0",
"sd": "20000101000000",
"vtyp": "1",
"sno": "0001",
},
{
"_id": "2",
"iid": "339",
"sv": "0",
"sd": "20000101000000",
"vtyp": "1",
"sno": "0001",
}
]
}
}
我只对“结果”中包含的对象数组感兴趣。从我能够找到的,我应该实现一个JsonDeserializer来获取该数据,并在构建RestAdapter时使用.setConverter。这就是我现在所拥有的:
class MyDeserializer implements JsonDeserializer<Pojo> {
@Override
public Station deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonElement element = json.getAsJsonObject().get("results");
return new Gson().fromJson(element, Pojo.class);
}
我希望改装GET调用返回
Observable<List<Pojo>>
表示“结果”中的对象
我的Pojo.class看起来像这样:
public class Pojo {
public String _id;
public String iid;
public String sv;
public String sd;
public String vtyp;
}
现在写我在尝试调用API时遇到此错误,所以我认为我在Json反序列化中出错:
retrofit.RetrofitError: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
任何帮助将不胜感激!
答案 0 :(得分:0)
您需要创建以下三个pojo类
MyPojo.java
public class MyPojo
{
private Result result;
public Result getResult ()
{
return result;
}
public void setResult (Result result)
{
this.result = result;
}
}
Result.java
public class Result
{
private String limit;
private String sort;
private Results[] results;
private String count;
private String offset;
public String getLimit ()
{
return limit;
}
public void setLimit (String limit)
{
this.limit = limit;
}
public String getSort ()
{
return sort;
}
public void setSort (String sort)
{
this.sort = sort;
}
public Results[] getResults ()
{
return results;
}
public void setResults (Results[] results)
{
this.results = results;
}
public String getCount ()
{
return count;
}
public void setCount (String count)
{
this.count = count;
}
public String getOffset ()
{
return offset;
}
public void setOffset (String offset)
{
this.offset = offset;
}
}
Results.java
public class Results
{
private String sno;
private String _id;
private String iid;
private String vtyp;
private String sd;
private String sv;
public String getSno ()
{
return sno;
}
public void setSno (String sno)
{
this.sno = sno;
}
public String get_id ()
{
return _id;
}
public void set_id (String _id)
{
this._id = _id;
}
public String getIid ()
{
return iid;
}
public void setIid (String iid)
{
this.iid = iid;
}
public String getVtyp ()
{
return vtyp;
}
public void setVtyp (String vtyp)
{
this.vtyp = vtyp;
}
public String getSd ()
{
return sd;
}
public void setSd (String sd)
{
this.sd = sd;
}
public String getSv ()
{
return sv;
}
public void setSv (String sv)
{
this.sv = sv;
}
}
在活动中写下以下代码
private RestAdapter restAdapter;
private ApiListeners apiListener;
restAdapter = new RestAdapter.Builder().setLogLevel(RestAdapter.LogLevel.FULL).setEndpoint("Your Base Url").build();
apiListener = restAdapter.create(ApiListeners.class);
apiListener.getData(new Callback<MyPojo>()
{
@Override
public void success(MyPojo pojo, Response response)
{
}
@Override
public void failure(RetrofitError error)
{
}
});
创建以下监听器
public interface ApiListeners
{
@GET("/yourApiTag")
public void getData(retrofit.Callback<MyPojo> responseVo);
}