1

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.

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Rakesh
  • 1,205
  • 1
  • 14
  • 33

2 Answers2

7

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.

Maddy
  • 4,525
  • 4
  • 33
  • 53
-1

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"

    }]
Ankit Patidar
  • 2,731
  • 1
  • 14
  • 22
Apoorv Singh
  • 1,295
  • 1
  • 14
  • 26