简体   繁体   English

如何解析包含嵌套 JSON 数组的 JSON 响应

[英]How to parse JSON response which contains nested JSON array

I'm trying to parse a JSON response from an API and store the data to DATA CLASS and sending the data to recycler adapter as ArrayList.我正在尝试解析来自 API 的 JSON 响应并将数据存储到 DATA CLASS 并将数据作为 ArrayList 发送到回收器适配器。

The JSON Array has another array of objects inside, and I'm not able to find a way to properly parse that JSON response. JSON 数组里面有另一个对象数组,我无法找到正确解析该 JSON 响应的方法。

Here is my data class:这是我的数据类:

data class OrderDetails (
val orderId: String, // order_id value from json object goes here //
val restaurantName: String, // restaurant_name value from json object goes here //
val totalCost: String, // total_cost value from json object goes here //
val orderDate: String, // order_placed_at value from json object goes here //
val orderFoodDetails: String // food_items value in json response is an array and i'm stuck here //
)

Here is my Kotlin code:这是我的 Kotlin 代码:

try {
val data = it.getJSONObject("data")
val success = data.getBoolean("success")

if (success) {
val arrayData = data.getJSONArray("data")
for (i in 0 until arrayData.length()) {
val orderJsonObject = arrayData.getJSONObject(i)
val orderObject = OrderDetails(
orderJsonObject.getString("order_id"),
orderJsonObject.getString("restaurant_name"),
orderJsonObject.getString("total_cost"),
orderJsonObject.getString("order_placed_at"),
orderJsonObject.getJSONArray("food_items").toString() // getting array and storing as a string
)
orderList.add(orderObject)

for (orders in orderList) {
val foodData = orders.orderFoodDetails
val jsonFood = JSONArray(foodData)
for (j in 0 until jsonFood.length()) {
val foodJsonObject = jsonFood.getJSONObject(j)
val foodObject = OrderFoodDetails(
foodJsonObject.getString("food_item_id"),
foodJsonObject.getString("name"),
foodJsonObject.getString("cost")
)
ordersFood.add(foodObject)
}
}
}

Here is the Json response:这是 Json 响应:

{
"data": {
"success": true,
"data": [
            {
"order_id": "17790",
"restaurant_name": "Rotten Tomatoes",
"total_cost": "280",
"order_placed_at": "02-11-20 19:00:54",
"food_items": [
                    {
"food_item_id": "156",
"name": "Rotten Bhajiya",
"cost": "100"
                    },
                    {
"food_item_id": "155",
"name": "Rotten Salad",
"cost": "100"
                    },
                    {
"food_item_id": "154",
"name": "Rotten Soup",
"cost": "80"
                    }
                ]
            },

Required Output Prefered Output所需输出首选输出

My output my current output我的输出我的当前输出

do you tried to use GSON or Moshy lib ?你试过使用 GSON 或 Moshy lib 吗? This case will be easier solved with one of this :)使用其中之一可以更轻松地解决这种情况:)

If you don't want to use one of them, just try to replace your for cycle to map or for each, to make it more readable.如果您不想使用其中之一,只需尝试将您的 for 循环替换为 map 或 for each,以使其更具可读性。 Looks like you make it right, but can you check the result of try catch block please?看起来你做对了,但是你能检查一下 try catch 块的结果吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM