提问人:Zulqarnain Haider 提问时间:9/8/2023 最后编辑:Jared SmithZulqarnain Haider 更新时间:9/8/2023 访问量:55
无法解析复杂的嵌套 JSON 颤振
Unable to parse complex nested JSON flutter
问:
我在 flutter 应用程序中获取时获得了空数据(场地详细信息、分支机构详细信息及其嵌套数据),但它在邮递员上显示非空数据,这意味着 api 给出了正确的响应,但函数无法获取它,这是我的函数,'
Future<bool> getPresentOrders({
required token,
required userId,
}) async {
try {
final response = await http.post(
Uri.parse(BASE_URL + '/api/get_present_orders'),
headers: {
"Content-type": "application/json",
"Accept": "application/json",
'Authorization': 'Bearer $token',
},
body: jsonEncode({
'user_id': userId,
}),
);
// final extractedData = json.decode(response.body);
final extractedData = json.decode(response.body)['success']
as List<dynamic>?; // Map<String, dynamic>;
print('getting pending orders $extractedData');
if (extractedData == null) {
return false;
}
if (response.statusCode == 200) {
final List<Order> loadedOrderList = [];
extractedData.forEach((prodData) {
if (
prodData != null &&
prodData['venue_detail'] != null &&
prodData['branch_detail'] != null) {
loadedOrderList.add(
Order(
venueId: prodData['venue_id'].toString(),
branchId: prodData['branch_id'].toString(),
orderNumber: prodData['order_unique_id'],
orderTime: prodData['order_time'],
total: prodData['total_price'],
orderType: prodData['order_type'],
currentStatus: prodData['current_status'],
venueName: prodData['venue_detail']['venue_name'],
venueType: prodData['venue_detail']['venue_type'],
branchName: prodData['branch_detail'] != null
? prodData['branch_detail']['name']
: 'Branch',
venueLogo:
prodData['venue_detail']['logo_url'],
orderItems: (json.decode(prodData['items_detail'])
// .substring(1, prodData['items_detail'].length - 1)
// .split(' ","')
as List<dynamic>)
.map((item) =>
// CartItem(id: '1', title: '2', quantity: 3, price: 4))
CartItem(
id: item['id'].toString(),
title: item['title'].toString(),
quantity: int.parse(item['quantity'].toString()),
price: double.parse(item['price'].toString()),
specialInstruction: item['instructions'].toString(),
))
.toList(),
// venueName:
),
);
}
});
// print('now printing my model data');
loadedOrderList.forEach((element) {});
_or = loadedOrderList.reversed.toList();
} else {
print('Something Went Wrong!');
}
notifyListeners();
return true;
} catch (error) {
rethrow;
}
}`
有人可以帮我解决这个问题吗?问题实际上发生在 venue details 和 branch details params 中,这是响应,请检查并提出解决方案。
{
"success": [ {
"id": 35,
"user_id": 1,
"order_unique_id": "7487",
"venue_id": 1,
"branch_id": 1,
"category_id": 1,
"sub_category_id": 1,
"items_detail": "[{\"id\":\"1\",\"title\":\"كراهي\",\"quantity\":1,\"price\":50.0,\"instructions\":\"\",\"image\":\"https://adminportal.urmenuz.com/public/uploads/menu_images/1689758678.jpg\"}]",
"total_price": "50.00",
"wallet_amount": "0.00",
"payment_method_amount": "50.00",
"service_fee": "0.00",
"additional_fee": null,
"payment_method": "cod",
"add_ons": "[]",
"table_number": null,
"order_type": "dine_in",
"curbside_pickup_details": null,
"additional_remarks": "xyz",
"reviews": "abc",
"order_time": "2023-08-19 13:10:07",
"in_kichen_time": null,
"order_ready_time": null,
"order_completion_time": null,
"current_status": "pending",
"is_rated": 0,
"is_paid": 0,
"cc_number": null,
"created_at": "2023-08-19T08:10:07.000000Z",
"updated_at": "2023-08-19T08:10:07.000000Z",
"venue_detail": {
"id": 1,
"customer_id": 1,
"venue_name": "Chilli Chutney",
"description": null,
"facebook_url": "https://www.facebook.com/",
"insta_url": "https://www.instagram.com",
"cuisine_type": null,
"venue_type": "FSR",
"venue_logo": "1690186873.png",
"banner": null,
"is_available": 1,
"status": "Active",
"view_type": "list_view",
"created_at": "2023-07-19T09:06:50.000000Z",
"updated_at": "2023-07-24T08:21:13.000000Z",
"logo_url": "https://adminportal.urmenuz.com/uploads/logos/1690186873.png",
"banner_url": "https://adminportal.urmenuz.com/uploads/banners/"
},
"branch_detail": {
"id": 1,
"venue_id": 1,
"name": "Salt Chilli Spy",
"country": "Saudi Arabia",
"city": "Dammam",
"latitude": "23.95555172",
"longitude": "42.51512301",
"closing_hour": null,
"attributes": null,
"facebook_url": "https://www.facebook.com/",
"insta_url": "https://www.instagram.com",
"dine_in_banner": "1689757787.jpg",
"pick_up_banner": "1689757777.jpg",
"branch_manager_name": "ali",
"branch_manager_email": "[email protected]",
"services_type": null,
"is_dine_in": 1,
"is_pick_up": 1,
"is_pick_up_curb": 0,
"is_available": 1,
"status": "Active",
"created_at": "2023-07-19T09:09:38.000000Z",
"updated_at": "2023-07-25T08:01:05.000000Z",
"banner_pickup_url": "https://adminportal.urmenuz.com/uploads/banners/1689757777.jpg",
"banner_dine_url": "https://adminportal.urmenuz.com/uploads/banners/1689757787.jpg"
},
"web_items": [
{
"id": "1",
"title": "كراهي",
"quantity": 1,
"price": 50,
"instructions": "",
"image": "https://adminportal.urmenuz.com/public/uploads/menu_images/1689758678.jpg"
}
],
"web_addons": []
}, ]}
答:
0赞
Ivo
9/8/2023
#1
我不知道这是否是唯一的问题,但有一件事是解码两次是错误的
orderItems: (json.decode(prodData['items_detail']) as List<dynamic>)
应该只是
orderItems: (prodData['items_detail'] as List<dynamic>)
最初的解码使它递归为地图和列表等
评论
0赞
Zulqarnain Haider
9/9/2023
不,问题出在venue_detail和branch_detail,所有其他数据都已正确获取
评论
"success"