如何使用 Retrofit 2.0 解析带有动态键的 json?

Rak*_*esh 1 android json retrofit2

下面是JSON其中jsonObject的关键是动态的:

{
  "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"
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

我想解析这个JSON。请让我知道应该如何级
与写@SerializedName注释使用Retrofit

注意CH000032, CH000033 etc are dynamic.

Sub*_*ati 7

您可以Map<String, ModelClassName>在模型类中使用动态,如下所示:-

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

    //....
}
Run Code Online (Sandbox Code Playgroud)

这有助于在改造中解析动态密钥。

  • 您的密钥名称是什么? (2认同)