I'm interested in converting a JSONObject to a java bean. Let's say I have the following example code:
public class Data {
String name;
String email;
String age;
String favoriteHobby;
}
JSONObject response = {'name' : 'alex', 'email' : 'alex@alex.com' : 'age' : '22'};
Gson gson = new Gson();
String json = response.toString();
Data data = gson.fromJson(json, Data.class);
This works fine except it doesn't do exactly what I want it to do. The following bit of code would set everything to whatever was in the JSONObject, but it would set favoriteHobby = null.
This doesn't represent the code I'm using in my program, but basically two parameters of my class get initialized to something before I use the fromJson code and are not present in the JSONObject like in the example above. I don't want those two parameters to be overwritten to null by the fromJson() method. I also don't want to have to call every members setters in order to initialize the bean.
Is there anything similar to the fromJson() method, but is a bit more flexible? Does anyone have suggestions on how to accomplish this?
Thank you.