I have json format:
{
"root": {
"user": {
"name":"Name",
"age":99,
"another_obj": {
"f1":"abc",
"f2":1
}
}
},
"result_code": 0
}
I am creating some model:
class User {
private String name;
private int age;
@SerializedName("detail")
private Detail
}
class Detail{
}
Finally, I create the class name UserWapper.
class UserWapper {
@SerializedName("root")
private User user;
}
To parse json:
String json = "STRING_JSON_ABOVE";
Gson gson = new Gson();
UserWrapper uw = gson.fromJson(json, UserWrapper.class);
But at the moment I do not want to create a Wrapper class. Because I have many classes. If you create a WrapperClass →: |
I want to do something like:
User u = gson.fromJson(json, User.class).
(Note: I can’t change the json format data from the server. I also looked for some custom gson adapter from gg, but I did not find a way to skip the “level” of another field).
source
share