手动使用改造时,解析响应的一部分 [英] Manually parse part of a response when using Retrofit

查看:144
本文介绍了手动使用改造时,解析响应的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正与一个REST API,它返回如下所示启动,包括包含ABC字符串ID项目的收藏一个JSON文件。注意路由字段,其中包含了一系列字段称为ABC的,ABD,ABE等,但是路由不会重新psented作为JSON数组$ P $,因此,所有这些

I'm working with a REST API that returns a JSON document that starts as follows and includes a "collection" of items with string IDs like "ABC". Note the "routes" field, which contains a series of fields called "ABC", "ABD", "ABE" etc, however routes is not represented as an array in the json, so all these

{
"status":true,
"page":1,
"per_page":500,
"total_count":1234,
"total_pages":8,
"total_on_page":500,
"routes":{
    "ABC":[
    {
        "value":22313,
        <......>

我使用的是改造和问题是路由域不是一个数组(尽管在概念上可以肯定的是),并改造/ GSON要求我创建一个模型对象线路与现场瓦尔ABC,ABD等 - 这是不实际的数据更改。由于种种原因,更改服务器API是很难的,所以我在寻找解决此Android客户端上。

I'm using Retrofit and the problem is the routes field is not an array (despite the fact conceptually it certainly is) and Retrofit/Gson require me to create a model object for routes with field vars abc, abd, etc - this is not practical as the data changes. For various reasons changing the server API is hard, so I'm looking to work around this on the Android client.

我想,这些都是选项:

  • 拦截才达到GSON和定制GSON解析器调整的文件,可能的话,或者通过拦截HTTP响应,将JSON文件

  • Intercept the JSON document before it reaches Gson and tweak the document, possibly with a customised Gson parser, or by intercepting the HTTP response.

绕过JSON解析,并获得从改造的JSON文件(我还没有搞清楚如何做到这一点,或者如果可能的话)

Bypass the JSON parsing, and acquire the JSON document from Retrofit (I've yet to figure out how to do this, or if it's possible)

使用改造的一些特点,我不知道到字段名称映射到集合。

Use some feature of Retrofit I'm unaware of to map field names to a collection.

我倒是AP preciate的帮助,尤其是如果有一个快速简便的方法来解决这个问题。

I'd appreciate help, especially if there's a quick and easy way to resolve this.

推荐答案

事实证明,改造的使用GSON的默认使得它很容易添加自定义解串器来处理JSON文档,这是问题的一部分。

It turns out that Retrofit's use of Gson by default makes it fairly easy to add a custom deserialiser to handle the portion of the JSON document that was the problem.

RestAdapter restAdapter = new RestAdapter.Builder()
        .setEndpoint(ApiDefinition.BASE_URL)
        .setConverter(getGsonConverter())
        .build();

public Converter getGsonConverter() {
    Gson gson = new GsonBuilder()
            .registerTypeAdapter(RouteList.class, new RouteTypeAdapter())
            .create();
    return  new GsonConverter(gson);
}


public class RouteTypeAdapter implements JsonDeserializer<RouteList> {
    @Override
    public RouteList deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        Gson gson = new Gson();
        RouteList routeList = new RouteList();
        JsonObject jsonObject = json.getAsJsonObject();
        for (Map.Entry<String,JsonElement> elementJson : jsonObject.entrySet()){
            RouteList wardsRoutes = gson.fromJson(elementJson.getValue().getAsJsonArray(), RouteList.class);
            routeList.addAll(wardsRoutes);
        }
        return routeList;
    }

}

这篇关于手动使用改造时,解析响应的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆