It may be a little late for the original poster, but hopefully it helps someone else.
I am using Gson on Android . I saw how everyone uses custom classes and long-path solutions. My main.
I have an ArrayList from many different types of objects (Models for my database). Profile is one of them. I get the element using mContactList.get(i) , which returns:
{"profile": {"name":"Josh", "position":"Programmer", "profile_id":1, "profile_image_id":10, "user_id":1472934469 }, "user": {"email":" example@you.co.za ", "phone_numbers":[], "user_id":1, "user_type_id":1 }, "follower": {"follower_id":3, "following_date":1.4729345E9, "referred_by_id":2, "user_from_id":1, "user_to_id":2 }, "media": {"link":"uploads/profiles/profile-photos/originals/1-G9FSkRCzikP4QFY.png", "media_description":"", "media_id":10, "media_name":"", "media_slug":"", "medium_link":"uploads/profiles/profile-photos/thumbs-medium/1-G9FSkRCzikP4QFY.png", "thumbnail_link":"uploads/profiles/profile-photos/thumbs-small/1-G9FSkRCzikP4QFY.png", "uploader_id":1 } }
Now I create a Gson object:
Gson gson = new Gson(); // this creates the JSON string you see above with all of the objects String str_obj = new Gson().toJson(mContactList.get(i));
Now instead of creating a custom class, just pass it as a JsonObject using the following code:
JsonObject obj = gson.fromJson(str_obj, JsonObject.class);
And now you can call the object as follows:
JsonObject profile = obj.getAsJsonObject("profile");