You should use a custom JsonDeserializer:
private class MyCustomDeserializer implements JsonDeserializer<MyModel> {
@Override
public MyCustomDeserializer deserialize(JsonElement json, Type type,
JsonDeserializationContext context) throws JsonParseException {
// initialize an instance of your model
MyModel myModel = new MyModel();
JsonArray jArray = (JsonArray) json; // get json array
JsonObject jsonObject = (JsonObject) jArray.get(0); // get first object
// do what you want with the first object
myModel.setParameter(jsonObject.get("parameter").getAsInt());
// ignore next json objects
return myModel;
}
}
Then, initialize your Gson
instance like this:
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(MyModel.class, new MyCustomDeserializer());
Gson gson = gsonBuilder.create();
MyModel model = gson.fromJson(jsonString, MyModel.class);
If you want to exclude some fields from Serialization you need to declare them in your model as transient
:
private transient String name; // will be ignored from Gson