【问题标题】:How to access value from nested JSON through gson in Kotlin?如何通过 Kotlin 中的 gson 访问嵌套 JSON 中的值?
【发布时间】:2023-03-03 01:44:01
【问题描述】:

我希望在这个 JSON 中访问flower_id id 的值。我想通过 gson 或 JSONObject 访问它。这是我的课程:

class Json2(val result: Result2) {
}
class Result2(val status:String, val featured_items: Array<FeaturedItems> , val trending_items:Array<TrendingItems> ) {
}
class FeaturedItems(val pic:String, val price:String, val item_name:String, val shop_name:String, val flower_id:String) {
}
class TrendingItems(val pic:String,val price:String, val item_name:String, val shop_name:String, val flower_id:String) {
}

这是我在 AsyncHttpResponseHandler() 的成功部分写的:

var jsonInfo2 = mutableListOf<FeaturedItems>()
    var jsonInfo3 = mutableListOf<TrendingItems>()

                override fun onSuccess(statusCode: Int, headers: Array<out Header>?, responseBody: ByteArray?) {

                    if (responseBody != null) { val charset2 = Charsets.UTF_8

                        val json2 = String(responseBody, charset2)
                        val gson2 = Gson().newBuilder().serializeNulls().create()

                        val homescreenresult = gson2.fromJson<Json2>(json2, genericType<Json2>())

                        jsonInfo2.addAll(homescreenresult.result.featured_items)

                        jsonInfo3.addAll(homescreenresult.result.trending_items)

这是我需要从中访问flower_id 的 JSON。

{ "result": { "status": "1", "top_icons": [ { "record_id": 0, "title": "", "status": true }, { "record_id": 1, "title": "Categories", "status": true }, { "record_id": 2, "title": "Shops", "status": true }, { "record_id": 3, "title": "Occasions", "status": true }, { "record_id": 4, "title": "Offers", "status": true } ], "banners": [ { "banner_id": "ubZFZJaaiEc=", "skip_button": false, "bg_color": "", "button_bg_color": "", "button_text_color": "", "button_text": "", "screen": "", "shop_id": "208", "shop_name": "Dear Cocoa", "occasion_id": "", "occasion_name": "", "item_id": "9ua2ojMRCWY=", "section_id": "2", "banner_text": "", "youtube_video_id": "" }, { "banner_id": "WrHTW4q7Aro=", "skip_button": false, "bg_color": "", "button_bg_color": "", "button_text_color": "", "button_text": "", "screen": "", "shop_id": "", "shop_name": "", "occasion_id": "97", "occasion_name": "Gergean", "item_id": "", "section_id": "2", "banner_text": "", "youtube_video_id": "" }, { "banner_id": "DFozQw8G9h0=", "skip_button": false, "bg_color": "", "button_bg_color": "", "button_text_color": "", "button_text": "", "screen": "", "shop_id": "592", "shop_name": "2u Store", "occasion_id": "", "occasion_name": "", "item_id": "", "section_id": "1", "banner_text": "", "youtube_video_id": "" } ], "sections": [ { "section_id": -1, "name": "New", "custom_val": "shops-new", "status": true }, { "section_id": 1, "name": "Flowers", "custom_val": "1", "status": true }, { "section_id": 2, "name": "Confections", "custom_val": "2", "status": true }, { "section_id": 3, "name": "Gifts", "custom_val": "3", "status": true } ], "featured_items": [ { "record_id": "Aizh3yL5+jw=", "flower_id": "3S8bQ9d31yw=", "product_id": 283946, "item_name": "25% OFF - Vero", "shop_name": "Flower and Beyond", "currency": "KWD", "price_per": 26.25, "price": "KWD 26.250", "old_price": "KWD 35.000", "sell_by_min": 1.0, "max_quantity": 6, "sell_by_unit": "1.0", "sell_by": "quantity", "same_day_delivery": true, "distance": "0 km" },

如您所见,热门商品和特色商品都有flower_id。我想访问两个类数组中的所有花 id,并在单击这两个项目中的任何一个时相应地使用它们。

【问题讨论】:

  • 具体问题是什么,换句话说,考虑到 GSON/JSON 库的文档非常完善,什么是行不通的?
  • 我无法从此 JSON 访问flower_id 的值。我正在使用模型类方法来访问不在列表中的其他键的值。我不知道如何访问数组列表中键的值。例如:“featured_items”有数组列表。从中我想要 "flower_id" 的值,将其存储在一个变量中,因为我需要将该值传递给一个 API,这将为我提供进一步的结果。
  • 请阅读Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - 总结是这不是解决志愿者的理想方式,并且可能会适得其反。请不要将此添加到您的问题中。

标签: android json kotlin gson


【解决方案1】:

为了使用 Gson 解析 JSON,您需要根据您拥有的花 JSON 结构定义自己的 JsonObject 类。 这些类可以嵌套。一个“Json 类”可以包含另一个作为其字段。例如:data class YourJsonClass(var id: Int, var data: List&lt;YourNestedJsonClass&gt;)
然后你需要像这样简单地解析 JSON:Gson().fromJson(yourJsonText, YourJsonClass::class.java)
Gson 会根据你写的结构自动解析。

【讨论】:

  • 我已经试过了。 val charset4 = Charsets.UTF_8 val json4 = String(responseBody, charset4) val gson4 = Gson().newBuilder().serializeNulls().create() val itemscreenresult = gson4.fromJson(json4, genericType( )) jsonInfo6.addAll(itemscreenresult.result.data)
  • 但我需要 Json4 来访问其他值。因此我无法访问其他类的值。
  • 你能帮忙吗?
  • 你说你试过这个,但我没有看到你的课程。这是给你的指南:sites.google.com/site/gson/gson-user-guide#TOC-Object-Examples
  • 我已经编辑了我的代码并显示了类。
猜你喜欢
  • 2021-10-07
  • 2018-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多