gpt4 book ai didi

java - 无法将 json 转换为 java pojo 类?

转载 作者:太空宇宙 更新时间:2023-11-03 13:07:49 24 4
gpt4 key购买 nike

我想将此 JSON 转换为一个列表并插入到 RecyclerView 中,但我不能,因为它给了我 3 个类。那么如何将此类转换为父类;任何人都可以帮助我,以便我可以列出要放入 RecyclerView 的响应。

[
{
"id": 114,
"name_ar": "محمصه زين",
"latitude": null,
"longitude": null,
"name_en": "Mohamsa Zein",
"description_ar": "",
"description_en": "",
"phone": null,
"mobile": "01110881085",
"address_ar": "مدينتى، Egypt",
"address_en": "مدينتى، Egypt",
"photo": "5b7958dc1c3661534679260.jpg",
"album": null,
"created_at": {
"date": "2018-08-19 11:47:40.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"updated_at": {
"date": "2018-10-23 11:00:22.000000",
"timezone_type": 3,
"timezone": "UTC"
}
},
{
"id": 184,
"name_ar": "محل عصائر فيرتاس",
"latitude": null,
"longitude": null,
"name_en": "Varieties For Juice",
"description_ar": "",
"description_en": "",
"phone": null,
"mobile": "01024229935",
"address_ar": "مدينتى، Egypt",
"address_en": "مدينتى، Egypt",
"photo": null,
"album": null,
"created_at": {
"date": "2018-10-28 11:40:54.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"updated_at": {
"date": "2019-01-02 08:36:04.000000",
"timezone_type": 3,
"timezone": "UTC"
}
}
]

最佳答案

我的做法是这样的:

使用 GSON 库。

我创建了 2 个模型类

  1. 包含每个数组项信息的主类
  2. 包含您的日期信息的自定义日期类created_atupdated_at 属性

    类 MyDate{
    公共(public)字符串日期;
    公共(public) int timezone_type;
    公共(public)字符串时区
    }

    类信息{
    公共(public)字符串标识;
    公共(public)字符串名称_ar;
    ..//其他属性//..
    public MyDate created_at;
    public MyDate updated_at;
    }

然后将json解析为Info类的数组列表

List<Info> infoArray = new Gson().fromJson(stringJson, new TypeToken<List<Info>>(){}.getType());

关于java - 无法将 json 转换为 java pojo 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54472832/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com