首页 > 解决方案 > 如何通过 Kotlin 中的 gson 访问嵌套 JSON 中的值?

问题描述

我正在寻找在这个 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,并在单击这两个项目中的任何一个时相应地使用它们。

标签: androidjsonkotlingson

解决方案


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


推荐阅读