如何使用Retrofit 2.0使用动态密钥解析json?

时间:2017-12-11 07:14:08

标签: 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"
      }
    }
  ]
}

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

注意CH000032, CH000033 etc are dynamic.

2 个答案:

答案 0 :(得分:3)

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

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

    //....
}

这可以帮助解析改造中的动态密钥。

答案 1 :(得分:0)

你所要求的将需要做很多工作,而更好的方法是将json的结构改为这样的东西 -

"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"

    }]