我的 Json 数据样本。这只是数据的示例。同样,可用的数据很多,例如子类别的数量为 22。项目数量根据子类别而不同。行数也是 15,其中第一个名称是 Pizza。
[
{
"id": "244",
"name": "PIZZAS",
"image": "",
"coupon": "1",
"icon": "",
"order": "1",
"aname": "",
"options": "2",
"subcategory": [
{
"id": "515",
"name": "MARGARITA",
"description": "Cheese and Tomato",
"image": "",
"icon": "",
"coupon": "1",
"order": "1",
"aname": "",
"options": "2",
"item": [
{
"id": "1749",
"name": "9 Inch Thin & Crispy Margarita",
"description": "",
"price": "3.40",
"coupon": "1",
"image": "",
"options": "2",
"order": "1",
"addon": "495",
"aname": "",
"icon": ""
}]
}]
}]
我想将所有名称、所有子类别名称、所有项目名称和所有项目描述提取到特定数组中,以便我可以特别填充到不同的表视图中
func ParseJSON(){
var MenuList = [Menu]()
Alamofire.request(.GET, myUrl)
.validate()
.responseJSON { response in
switch response.result
{
case .Success:
if let value = response.result.value {
let json = JSON(value)
print("JSON: \(json)")
for entity in json{
print(entity)
MenuList.append(entity["name"]) ///error
}
}
case .Failure(let error):
print(error)
}
}
}
我可以使用 Alamofire 获取响应。现在如何使用 swiftJson 来实现它?
Updated
我添加了主类别、子类别和项目的类模型。
import UIKit
class Menu {
var id: Int?
var name: String?
var image: UIImage?
var coupon: Int?
var icon: UIImage?
var order: Int?
var aname: Int?
var options: Int?
init(id:Int,name:String,image:UIImage,coupon:Int,icon:UIImage,order:Int,aname:Int,options:Int){
self.id = id
self.name = name
self.image = image
self.coupon = coupon
self.icon = icon
self.order = order
self.aname = aname
self.options = options
}
}
如何将数据传递给类并获取值