Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Parse Json Object with dynamic keys using Gson

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 16.5k
    Comment on it

    Gson is a java library which converts Json to Java object and vice versa.

    This blog is about how we can parse Json data to Java Object where keys are dynamic created.

    1. Json Sample text is:-

    1. {
    2.     "data": {
    3.         "status_code": "101",
    4.         "feeds": {
    5.             "39": {
    6.                 "name": "Ammy",
    7.                 "address": "Dehradun"
    8.             },
    9.             "40": {
    10.                 "name": "Aman",
    11.                 "address": "Dehradun"
    12.             }
    13.         }
    14.     }
    15. }

    2. Create Java class with name DemoJson and Add below code.

    1. @SerializedName("data")
    2. @Expose
    3. private Data data;
    4.  
    5. /**
    6. *
    7. * @return
    8. * The data
    9. */
    10. public Data getData() {
    11. return data;
    12. }
    13.  
    14. /**
    15. *
    16. * @param data
    17. * The data
    18. */
    19. public void setData(Data data) {
    20. this.data = data;
    21. }
    22.  
    23. }

    3. Create Class with name Data as below.

    1. public class Data {
    2.  
    3. @SerializedName("status_code")
    4. @Expose
    5. private String statusCode;
    6. @SerializedName("feeds")
    7. @Expose
    8. private Map<String, Feeds> feeds;
    9.  
    10. /**
    11. *
    12. * @return
    13. * The statusCode
    14. */
    15. public String getStatusCode() {
    16. return statusCode;
    17. }
    18.  
    19. /**
    20. *
    21. * @param statusCode
    22. * The status_code
    23. */
    24. public void setStatusCode(String statusCode) {
    25. this.statusCode = statusCode;
    26. }
    27.  
    28. /**
    29. *
    30. * @return
    31. * The feeds
    32. */
    33. public Map<String, Feeds> getFeeds() {
    34. return feeds;
    35. }
    36.  
    37. /**
    38. *
    39. * @param feeds
    40. * The feeds
    41. */
    42. public void setFeeds(Map<String, Feeds> feeds) {
    43. this.feeds = feeds;
    44. }
    45.  
    46. }

    4. Create Java class with name Feeds like below.

    1. public class Feeds {
    2.  
    3. @SerializedName("name")
    4. @Expose
    5. private String name;
    6. @SerializedName("address")
    7. @Expose
    8. private String address;
    9.  
    10. /**
    11. *
    12. * @return
    13. * The name
    14. */
    15. public String getName() {
    16. return name;
    17. }
    18.  
    19. /**
    20. *
    21. * @param name
    22. * The name
    23. */
    24. public void setName(String name) {
    25. this.name = name;
    26. }
    27.  
    28. /**
    29. *
    30. * @return
    31. * The address
    32. */
    33. public String getAddress() {
    34. return address;
    35. }
    36.  
    37. /**
    38. *
    39. * @param address
    40. * The address
    41. */
    42. public void setAddress(String address) {
    43. this.address = address;
    44. }
    45.  
    46. }

    5. Now open Class where you want to parse Json Data to Java Object.

    6. Create Gson object.

    1. Gson gson = new Gson();

    7. Get JavaObject from Below Code.

    1. DemoJson myObj = gson.fromJson(jsonData,
    2. new TypeToken<DemoJson>(){}.getType());
    3.  


    You will get all your Json data in myObj.

    Happy Coding :D

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: