1

I receive JSON response in the form of the following:

{
    "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,
                                ...
                            }
                        }]
                    }
                }]
            }
        }
    }
}

What would be the best way to create and organize my POJOs?

Thanks!

Draško
  • 2,119
  • 4
  • 41
  • 73
  • 1
    try using http://www.jsonschema2pojo.org/ – vaibhav May 11 '16 at 13:13
  • No, I can't use it, since I don't know structure in advance. – Draško May 11 '16 at 13:19
  • the JSON structure looks out of sorts, at the top level, you have MultiItemDetails, with Items which is an array of MultiItemDetails... – t0mm13b May 11 '16 at 13:19
  • @Deveti Putnik I think your `response` is not Valid Json check it. –  May 11 '16 at 13:19
  • @Harshad, I "translated" real JSON to something more general and maybe forgot somewhere } or ]. But basically, this is the structure I receive from server. – Draško May 11 '16 at 18:40

3 Answers3

3

Model your classes like this

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;
}

refactor the field names exactly to your json

Rajesh Batth
  • 1,672
  • 2
  • 19
  • 23
  • I applied this model and everything works smoothly without implementing deserializer. Good thing about Retrofit (_**and the one applied here**_) is that **it just doesn't take into account if some of the data from model are not given in response**. Thanks, @RajeshBatth! – Draško May 11 '16 at 18:33
0

You would write a custom deserializer that returns the embedded object.

Let's say your JSON is:

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

You'd then have a Content POJO:

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

Then you write a deserializer:

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);

    }
}

Now if you construct a Gson with GsonBuilder and register the deserializer:

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

for more detail visit this : Get nested JSON object with GSON using retrofit

Community
  • 1
  • 1
0

I think it can even easier, and you dont even need to use that JsonDesirializer class. Just let retrofit do everything for you. It was designed to handle this JSON Rest calls very well.

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;
}
Sergio Lima
  • 114
  • 13