If you can't change your model, i.e. MyClass
, you will have to write a custom deserializer.
See How do I write a custom JSON deserializer for Gson? for example.
In your case, it could be something like this :
@Override
public MyClass deserialize(JsonElement json, Type type,
JsonDeserializationContext context) throws JsonParseException {
JsonObject jobject = json.getAsJsonObject();
String item1 = jobject.get("item1").getAsString();
String item4 = jobject.get("item2").getAsJsonObject().get("item4").getAsString(); // TODO check for NPE
return new MyClass(item1, item4);
}
EDIT : you don't have to deserialize everything manually, if instead of String
s you have complex objects you can still partially invoke Gson on these objects (as mentionned in the link above).