Retrofit 2.0反序列化复杂的数据结构

时间:2016-05-11 13:05:01

标签: android json retrofit2

我以下列形式收到JSON回复:

{
    "result" : {
        "status" : 0,
        "resultItems" : {
            "MultiItemDetails" : {
                "min" : 3,
                ...
                "items" : [{
                    "SingleItem" : {
                        "active" : false,
                        "type" : 2,
                        ...
                    }
                }, {
                    "MultiItemDetails" : {
                        "min" : 1,
                        ...
                        "items": [{
                            "SingleItem" : {
                                "active" : true,
                                "type" : 1,
                                ...
                            }
                        },{
                            "SingleItem" : {
                                "active" : true,
                                "type" : 2,
                                ...
                            }
                        }]
                    }
                }]
            }
        }
    }
}

创建和组织我的POJO的最佳方法是什么?

谢谢!

3 个答案:

答案 0 :(得分:2)

像这样对你的类进行建模

class Response{
  Result result;
}

class Result{
  int status;
  List<Item> resultItems;
}

class Item{
  MultiItem multiItemDetails;
  SingleItem singleItem;
}

class MultiItem{
  List<Item> items;
  int min;
}

class SingleItem{
  boolean active;
  int type;
}

将字段名称精确重构为json

答案 1 :(得分:0)

您可以编写一个返回嵌入对象的自定义反序列化器。

假设你的JSON是:

{
    "status":"OK",
    "reason":"some reason",
    "content" : 
    {
        "foo": 123,
        "bar": "some value"
    }
}

然后你有一个Content POJO:

class Content
{
    public int foo;
    public String bar;
}

然后你写了一个反序列化器:

class MyDeserializer implements JsonDeserializer<Content>
{
    @Override
    public Content deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
        throws JsonParseException
    {
        // Get the "content" element from the parsed JSON
        JsonElement content = je.getAsJsonObject().get("content");

        // Deserialize it. You use a new instance of Gson to avoid infinite recursion
        // to this deserializer
        return new Gson().fromJson(content, Content.class);

    }
}

现在,如果您使用Gson构建GsonBuilder并注册反序列化器:

Gson gson = 
    new GsonBuilder()
        .registerTypeAdapter(Content.class, new MyDeserializer())
        .create();

有关详细信息,请访问:Get nested JSON object with GSON using retrofit

答案 2 :(得分:0)

我认为它甚至可以更容易,你甚至不需要使用那个JsonDesirializer类。只是让改造为你做一切。它旨在很好地处理这个JSON Rest调用。

public class RetrofitService {

ContentService mContentService;

public RetrofitService() {

    // ... init stuff
    // Retrofit retrofit = new Retrofit.Builder()

    mContentService = retrofit.create(ContentService.class);
}

public Content getContent() throws IOException {

 Call<Result<Content>> result = mContentService.getResult();

 retrofit2.Response<LResult<Content>> execute = result.execute();

 return execute.body().content;

}



public interface ContentService {
   @GET("somerestmethod")
   Call<Result<Content>> getResult();
}



public class Result<T> {

 @SerializedName("status")
 @Expose
 public String status;

 @SerializedName("content")
 @Expose
 public T content;
}

class Content{

 @SerializedName("foo")
 @Expose
 public int foo;
 @SerializedName("bar")
 @Expose
 public String bar;
}