I have a question
I'm trying to parse JSON response from API and storing the data to DATA CLASS and sending the data to recycler adapter as 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.
Any help????
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:
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:
{
"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"
}
]
},
{
"order_id": "17789",
"restaurant_name": "Rotten Tomatoes",
"total_cost": "280",
"order_placed_at": "02-11-20 19:00:29",
"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"
}
]
},
{
"order_id": "17690",
"restaurant_name": "Garbar Burgers",
"total_cost": "750",
"order_placed_at": "01-11-20 14:20:01",
"food_items": [
{
"food_item_id": "17",
"name": "Galti se Burger",
"cost": "140"
},
{
"food_item_id": "8",
"name": "No Burger",
"cost": "180"
},
{
"food_item_id": "7",
"name": "No Patty Burger",
"cost": "190"
},
{
"food_item_id": "6",
"name": "Burger from Nothing",
"cost": "140"
},
{
"food_item_id": "5",
"name": "Kabhi Burger Kabhi Garber",
"cost": "100"
}
]
},
{
"order_id": "17531",
"restaurant_name": "Garbar Burgers",
"total_cost": "160",
"order_placed_at": "28-10-20 17:12:36",
"food_items": [
{
"food_item_id": "20",
"name": "Salty Honey Burger",
"cost": "160"
}
]
},
{
"order_id": "17528",
"restaurant_name": "Pind Tadka",
"total_cost": "30",
"order_placed_at": "28-10-20 17:03:38",
"food_items": [
{
"food_item_id": "11",
"name": "Roti Tadka",
"cost": "30"
}
]
},
{
"order_id": "17517",
"restaurant_name": "Garbar Burgers",
"total_cost": "180",
"order_placed_at": "28-10-20 12:44:31",
"food_items": [
{
"food_item_id": "8",
"name": "No Burger",
"cost": "180"
}
]
},
{
"order_id": "17515",
"restaurant_name": "Heera Mahal",
"total_cost": "220",
"order_placed_at": "28-10-20 12:40:01",
"food_items": [
{
"food_item_id": "45",
"name": "Jogger Jagger Shake",
"cost": "220"
}
]
},
{
"order_id": "17514",
"restaurant_name": "Heera Mahal",
"total_cost": "220",
"order_placed_at": "28-10-20 12:39:47",
"food_items": [
{
"food_item_id": "45",
"name": "Jogger Jagger Shake",
"cost": "220"
}
]
},
{
"order_id": "17513",
"restaurant_name": "Heera Mahal",
"total_cost": "80",
"order_placed_at": "28-10-20 12:39:40",
"food_items": [
{
"food_item_id": "46",
"name": "Chota Pav",
"cost": "80"
}
]
},
{
..............................
Required output:
My output:
I'm trying to parse JSON response from API and storing the data to DATA CLASS and sending the data to recycler adapter as 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.
Any help????
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:
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:
{
"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"
}
]
},
{
"order_id": "17789",
"restaurant_name": "Rotten Tomatoes",
"total_cost": "280",
"order_placed_at": "02-11-20 19:00:29",
"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"
}
]
},
{
"order_id": "17690",
"restaurant_name": "Garbar Burgers",
"total_cost": "750",
"order_placed_at": "01-11-20 14:20:01",
"food_items": [
{
"food_item_id": "17",
"name": "Galti se Burger",
"cost": "140"
},
{
"food_item_id": "8",
"name": "No Burger",
"cost": "180"
},
{
"food_item_id": "7",
"name": "No Patty Burger",
"cost": "190"
},
{
"food_item_id": "6",
"name": "Burger from Nothing",
"cost": "140"
},
{
"food_item_id": "5",
"name": "Kabhi Burger Kabhi Garber",
"cost": "100"
}
]
},
{
"order_id": "17531",
"restaurant_name": "Garbar Burgers",
"total_cost": "160",
"order_placed_at": "28-10-20 17:12:36",
"food_items": [
{
"food_item_id": "20",
"name": "Salty Honey Burger",
"cost": "160"
}
]
},
{
"order_id": "17528",
"restaurant_name": "Pind Tadka",
"total_cost": "30",
"order_placed_at": "28-10-20 17:03:38",
"food_items": [
{
"food_item_id": "11",
"name": "Roti Tadka",
"cost": "30"
}
]
},
{
"order_id": "17517",
"restaurant_name": "Garbar Burgers",
"total_cost": "180",
"order_placed_at": "28-10-20 12:44:31",
"food_items": [
{
"food_item_id": "8",
"name": "No Burger",
"cost": "180"
}
]
},
{
"order_id": "17515",
"restaurant_name": "Heera Mahal",
"total_cost": "220",
"order_placed_at": "28-10-20 12:40:01",
"food_items": [
{
"food_item_id": "45",
"name": "Jogger Jagger Shake",
"cost": "220"
}
]
},
{
"order_id": "17514",
"restaurant_name": "Heera Mahal",
"total_cost": "220",
"order_placed_at": "28-10-20 12:39:47",
"food_items": [
{
"food_item_id": "45",
"name": "Jogger Jagger Shake",
"cost": "220"
}
]
},
{
"order_id": "17513",
"restaurant_name": "Heera Mahal",
"total_cost": "80",
"order_placed_at": "28-10-20 12:39:40",
"food_items": [
{
"food_item_id": "46",
"name": "Chota Pav",
"cost": "80"
}
]
},
{
..............................
Required output:
My output: