Rakesh
Rakesh

Reputation: 1236

How to parse json with dynamic keys using Retrofit 2.0?

Here's the JSON where jsonObject key is dynamic :

{
  "CH000032": [
    {
      "type": "event",
      "details": {
        "programID": "MV10000032",
        "programType": "MOVIE",
        "title": "Titanic",
        "year": "1997",
        "rating": "PG-13",
        "durationSec": 11640,
        "startTimeSec": "",
        "endTimeSec": "",
        "language": "ENG",
        "isHD": true,
        "Genres": [
          "Movies",
          "Action"
        ],
        "description": "A seventeen-year-old aristocrat falls in love with a kind but poor artist aboard the luxurious, ill-fated R.M.S. Titanic.",
        "imageUrl": "http://res.cloudinary.com/dte07foms/image/upload/c_scale,h_405,w_270/l_Copyright_e3jt1k/v1508831090/Titanic_b0hqo0.jpg"
      }
    }
  ],
  "CH000033": [
    {
      "type": "event",
      "details": {
        "programID": "EP10000132",
        "programType": "EPISODE",
        "title": "A Chic Bar in Ibiza",
        "seriesTitle": "Two and a Half Men",
        "seasonNumber": 12,
        "epsiodeNumber": 2,
        "year": "2014",
        "rating": "TV-14",
        "durationSec": 1260,
        "startTimeSec": "",
        "endTimeSec": "",
        "language": "ENG",
        "isHD": true,
        "Genres": [
          "Comedy",
          "Romance"
        ],
        "description": "Alan has second thoughts about getting married when Walden has him sign a prenup.",
        "imageUrl": "http://res.cloudinary.com/dte07foms/image/upload/c_crop,h_405,w_270//l_Copyright_e3jt1k/v1508831090/2AndHalfmen_splkro.jpg"
      }
    }
  ]
}

I would like to parse this JSON. Please let me know how should the class
be written with @SerializedName annotation using Retrofit.

Note : CH000032, CH000033 etc are dynamic.

Upvotes: 1

Views: 2163

Answers (2)

Subhash Prajapati
Subhash Prajapati

Reputation: 196

You can use Map<String, ModelClassName> in your model class for dynamic like below :-

public class Data {
    @SerializedName("your_key")
    @Expose
    private Map<String, ModelClassName> result;

    //....
}

this can help to parse dynamic key in retrofit.

Upvotes: 7

Apoorv Singh
Apoorv Singh

Reputation: 1335

What you are asking will take much work , instead a better approach will be to change the structure o json to something like this -

"data": [
    {
        "type": "event",

         "programID": "MV10000032",
         "programType": "MOVIE",
         "title": "Titanic",
         "year": "1997",
         "rating": "PG-13",
         "durationSec": 11640,
         "startTimeSec": "",
         "endTimeSec": "",
         "language": "ENG",
         "isHD": true,
         "Genres": [
           "Movies",
           "Action"
         ],
        "description": "A seventeen-year-old aristocrat falls in love with a kind but poor artist aboard the luxurious, ill-fated R.M.S. Titanic.",
        "imageUrl": "http://res.cloudinary.com/dte07foms/image/upload/c_scale,h_405,w_270/l_Copyright_e3jt1k/v1508831090/Titanic_b0hqo0.jpg"

    },

    {
        "type": "event",

         "programID": "MV10000032",
         "programType": "MOVIE",
         "title": "Titanic",
         "year": "1997",
         "rating": "PG-13",
         "durationSec": 11640,
         "startTimeSec": "",
         "endTimeSec": "",
         "language": "ENG",
         "isHD": true,
         "Genres": [
         "Movies",
           "Action"
        ],
        "description": "A seventeen-year-old aristocrat falls in love with a kind but poor artist aboard the luxurious, ill-fated R.M.S. Titanic.",
        "imageUrl": "http://res.cloudinary.com/dte07foms/image/upload/c_scale,h_405,w_270/l_Copyright_e3jt1k/v1508831090/Titanic_b0hqo0.jpg"

    }]

Upvotes: -1

Related Questions