如何使用Android中的Retrofit lib解析api的响应?
{
"elements": {
"12": {
...
},
"23": {
...
},
"32": {
...
}
},
"more": null
}
这里的元素是一个json对象,但需要作为数组。
这是我正在尝试的
public static class ElementResponseDeserializer implements JsonDeserializer<ElementResponse>{
@Override
public ElementResponse deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
logger.d("JSON >>>> typeOfT" + typeOfT.toString());
logger.d("JSON >>>> context" + context.toString());
logger.d("JSON >>>> " + json.toString());
JsonObject object = json.getAsJsonObject();
object.get
return new ElementResponse();
}
}
使用这种方式
Gson gson = new GsonBuilder()
.registerTypeAdapter(ElementsResponse.class, new ElementsResponseDeserializer())
.create();
RestAdapter restAdapter = new RestAdapter.Builder()
.setClient(new OkClient(client))
.setEndpoint(URL_API)
.setConverter(new GsonConverter(gson))
.setRequestInterceptor(new ApiRequestInterceptor())
.build();
restAdapter.setLogLevel(RestAdapter.LogLevel.FULL);
return restAdapter.create(MyApp.class);