从 JSON 数组中提取数据以创建 3 个不同的数组
Posted
技术标签:
【中文标题】从 JSON 数组中提取数据以创建 3 个不同的数组【英文标题】:Pulling Data from JSON Array to make 3 different Arrays 【发布时间】:2018-05-14 17:11:08 【问题描述】:我正在尝试根据从餐厅提取的 JSON 数据创建三个数组。我想要一个 Entre、Main 和 Dessert 数组,它们将显示在 tableView 对象中。这是我用来提取数据的代码:
func loadMeals()
Helpers.showActivityIndicator(activityIndicator, view)
if let restaurantId = restaurant?.id
APIManager.shared.getMeals(restaurantId: restaurantId, completionHandler: (json) in
if json != nil
self.meals = []
if let tempMeals = json["meals"].array
for item in tempMeals
let meal = Meal(json: item)
self.meals.append(meal)
self.tableView.reloadData()
Helpers.hideActivityIndicator(self.activityIndicator)
)
项目打印出来:
items
"name" : "Spinach Artichoke",
"course" : "entres",
"short_description" : "savory",
"id" : 20,
"image" : "http:\/\/localhost:8000\/media\/product_images\/artichoke.jpg",
"usage" : "homestyle",
"sizes" : [
"size" : 3,
"id" : 24,
"product_id" : 20,
"price" : 55.899999999999999
,
"size" : 4,
"id" : 25,
"product_id" : 20,
"price" : 78
,
"size" : 5,
"id" : 26,
"product_id" : 20,
"price" : 125
]
items
"name" : "Pizza",
"course" : "main",
"short_description" : "Melty cheese",
"id" : 19,
"image" : "http:\/\/localhost:8000\/media\/product_images\/pizza.jpg",
"usage" : "top",
"sizes" : [
"size" : 6,
"id" : 23,
"product_id" : 19,
"price" : 75.989999999999995
]
items
"name" : "Chocolate Mousee Devil's cake",
"course" : "dessert",
"short_description" : "Sweet And Smooth",
"id" : 18,
"image" : "http:\/\/localhost:8000\/media\/product_images\/Devils_cake.jpg",
"usage" : "sweets",
"sizes" : [
"size" : 2,
"id" : 20,
"product_id" : 18,
"price" : 50
,
"size" : 3,
"id" : 21,
"product_id" : 18,
"price" : 120
,
"size" : 4,
"id" : 22,
"product_id" : 18,
"price" : 376
]
我试图通过从这个函数中提取数据来弄清楚如何创建数组。任何帮助将不胜感激。
【问题讨论】:
您希望您的阵列是什么样的?能举个格式例子吗? 【参考方案1】:你只需要group By course
meals 数组将是 [[String:Any]]
键将是 course
Any
将是 course
项目数组
func loadMeals()
Helpers.showActivityIndicator(activityIndicator, view)
if let restaurantId = restaurant?.id
APIManager.shared.getMeals(restaurantId: restaurantId, completionHandler: (json) in
if json != nil
self.meals = []
if let tempMeals = json["meals"].array
self.meals = Dictionary(grouping: tempMeals, by: $0["course"] as! String )
self.tableView.reloadData()
Helpers.hideActivityIndicator(self.activityIndicator)
)
输出控制台:
["Main": [["name": Chocolate Mousee Devil's cake, "course": Main, "short_description": Sweet And Smooth, "id": 18, "image": http://localhost:8000/media/product_images/Devils_cake.jpg, "usage": sweets, "sizes": <__NSArrayI 0x60c0002456a0>(
id = 20;
price = 50;
"product_id" = 18;
size = 2;
,
id = 21;
price = 120;
"product_id" = 18;
size = 3;
,
id = 22;
price = 376;
"product_id" = 18;
size = 4;
)
]], "dessert": [["name": Chocolate Mousee Devil's cake, "course": dessert, "short_description": Sweet And Smooth, "id": 18, "image": http://localhost:8000/media/product_images/Devils_cake.jpg, "usage": sweets, "sizes": <__NSArrayI 0x60c000243d50>(
id = 20;
price = 50;
"product_id" = 18;
size = 2;
,
id = 21;
price = 120;
"product_id" = 18;
size = 3;
,
id = 22;
price = 376;
"product_id" = 18;
size = 4;
)
], ["name": Chocolate Mousee Devil's cake, "course": dessert, "short_description": Sweet And Smooth, "id": 18, "image": http://localhost:8000/media/product_images/Devils_cake.jpg, "usage": sweets, "sizes": <__NSArrayI 0x60c000248910>(
id = 20;
price = 50;
"product_id" = 18;
size = 2;
,
id = 21;
price = 120;
"product_id" = 18;
size = 3;
,
id = 22;
price = 376;
"product_id" = 18;
size = 4;
)
]]]
【讨论】:
我打算在 tableview 中使用分隔符来宣布特定的餐点。如果数组显示所有项目,是否可以这样做? 我将您的数据拆分为键,项目确实按课程键将您的代码排列到部分表中 我现在正在尝试这个,但是“jsonDictionary”似乎不是一个有效的键。我已经导入了 swiftyJson,但它似乎仍然不起作用 抱歉 jsonDicyionary 是我自己的 我修复它检查更新 谢谢。当你弄清楚时告诉我。以上是关于从 JSON 数组中提取数据以创建 3 个不同的数组的主要内容,如果未能解决你的问题,请参考以下文章
如何从 json 中提取数据并在 Next.js 中作为 props 传递