如何在Kotlin中通过gson访问嵌套JSON中的值?

pprl5pva  于 2022-11-06  发布在  Kotlin
关注(0)|答案(1)|浏览(280)

我想访问这个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" },
正如您所看到的,trending和featured项目都有flower_id。我希望访问这两个类的数组中的所有flower id,并在单击这两个项目中的任何一个时相应地使用它们。

r1zhe5dt

r1zhe5dt1#

为了用Gson解析JSON,你需要根据你拥有的flowers JSON结构定义你自己的JsonObject类。这些类可以嵌套。一个“Json类”可以包含另一个作为它的字段。例如:data class YourJsonClass(var id: Int, var data: List<YourNestedJsonClass>)
然后,您只需像下面这样解析JSON:Gson().fromJson(yourJsonText, YourJsonClass::class.java)
Gson会根据您编写的结构自动解析所有内容。

相关问题