JSON, JSON. "content" , :
{
"id": 2,
"name": "Test",
"content": {
"type": "status",
"text": "Lorem ipsum dummy text.",
"id": 1
}
}
gson.fromJson(response, TestModel.class), RetroFit GsonConverterFactory .
, , JSON, . , , , , , . , , content TestModel String:
class TestModel {
public int id;
public String name;
public String content;
}
class Content {
public int id;
public String type;
public String text;
}
:
TestModel testModel = gson.fromJson(response, TestModel.class);
Content content = gson.fromJson(testModel.content, Content.class);
, TypeAdapter content:
public class ContentAdapter extends TypeAdapter<Content> {
@Override
public void write(JsonWriter out, Content value) throws IOException {
}
@Override
public Content read(JsonReader in) throws IOException {
if(in.peek() != JsonToken.NULL) {
return fromJson(in.nextString());
} else {
in.nextNull();
return null;
}
}
}
TypeAdapter GSON:
Gson gson = new GsonBuilder()
.registerTypeAdapter(Content.class, new ContentAdapter()).create();