【问题标题】:Parsing Json with dynamic keys Using Retrofit 2.0使用 Retrofit 2.0 使用动态键解析 Json
【发布时间】:2017-05-25 13:41:09
【问题描述】:
{
    "Response": {
        "Header": {
            "StatusCode": 100,
            "Status": "Success",
            "Desc": "Success"
        },
        "Data": {
            "ComparisionsOfReportProducts": {
                "Datewise": [
                    {
                        "ReportDate": "04/11/2017",
                        "BT": "909",
                        "Ee": "1168",
                        "MG": "619",
                        "PY": "973",
                        "PF": "894",
                        "PA": "768",
                        "PX": "943"
                    },
                    {
                        "ReportDate": "04/14/2017",
                        "BT": "889",
                        "Ee": "1186",
                        "MG": "599",
                        "PY": "953",
                        "PF": "874",
                        "PA": "786",
                        "PX": "961"
                    },
                    {
                        "ReportDate": "04/16/2017",
                        "BT": "1162",
                        "Ee": "932",
                        "MG": "872",
                        "PY": "1226",
                        "PF": "1147",
                        "PA": "532",
                        "PX": "707"
                    },
                    {
                        "ReportDate": "04/18/2017",
                        "BT": "1020",
                        "Ee": "1055",
                        "MG": "730",
                        "PY": "1084",
                        "PF": "1005",
                        "PA": "655",
                        "PX": "830"
                    }
                ]
            }
        }
    }
}

我需要解析上面的Json。

 @FormUrlEncoded
    @POST("services/webservices/")
    Observable<ResComparision> getcomparisionDetail(@Field(value = "encAuthToken", encoded = true)String token,
                                                    @Field(value = "itemId", encoded = true) String productId,
                                                    @Field(value = "FromDate", encoded = true)String sdate,
                                                    @Field(value = "ToDate", encoded = true)String edate);

为了解析它们,我制作了 One Pojo 类。 但问题是“Datewise”jsons 数组的键是动态的,在我当前的 pojo 类中,我采用了预定义的键,因此无法解析它们。

  {
                                "ReportDate": "04/18/2017",
                                "BT": "1020",
                                "Ee": "1055",
                                "MG": "730",
                                "PY": "1084",
                                "PF": "1005",
                                "PA": "655",
                                "PQA": "655",
                                "PX": "830"
                            },
   {
                                "ReportDate": "04/18/2017",
                                "PP": "1020",
                                "QB": "1055",
                                "MG": "730",
                                "PY": "1084",
                                "PF": "1005",
                                "PA": "655",
                                "VA": "655",
                                "PX": "830"
                            }

有没有办法动态解析“Datewise”json 数组的值?

【问题讨论】:

标签: android retrofit2


【解决方案1】:

使用 GSON 来解析这个。对于 pojo 类使用这个站点。 http://www.jsonschema2pojo.org/

在下面选择 源类型:JSON, 注解风格:Gson

点击预览,您的 pojo 课程将准备就绪。

【讨论】:

  • 是的,我用这个,但事情是我的 Json 响应键是动态的。
  • 你将如何为动态 json 生成它?
猜你喜欢
  • 2016-02-14
  • 2018-05-24
  • 2016-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-19
  • 1970-01-01
相关资源
最近更新 更多