-2
樣品JSON數據抓取類模型對象的陣列是如何使用迅速
[
{
"id": "244",
"name": "PIZZAS",
"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": ""
}]
}]
}]
我的JSON數據是不準確的。有多少個子類別和項目。我只在這裏發佈樣本數據。
我可以將主要項目獲取到類模型中。但是,如何獲取子類別數據以及項目數據。因爲它是一個對象數組。 我創建的類模型這樣
子目錄類
import Foundation
class SubCategory {
var id: Int?
var name: String?
var desc: String?
var image: String?
var coupon: Int?
var icon: String?
var order: Int?
var aname: String?
var options: Int?
var items:Array<AnyObject>?
init(id:Int?,name:String?,desc:String?,image:String?,coupon:Int?,icon:String?,order:Int?,aname:String?,options:Int?,items:Array<AnyObject>?){
self.id = id
self.name = name
self.desc = name
self.image = image
self.coupon = coupon
self.icon = icon
self.order = order
self.aname = aname
self.options = options
self.items = items
}
}
我送的子類數據的類模型之後。但主要數據正在發佈。我知道我在這裏犯錯誤。但我怎麼能進入子類別部分
//subcategory section
var subcategories = [SubCategory]()
for(_,content) in json{ ////here instead of json what should be written so that i can get the subcategory value
let subcategory = SubCategory(id: Int(content["id"].stringValue),
name: content["name"].string,
desc: content["desc"].string,
image: content["image"].string,
coupon: Int(content["coupon"].stringValue),
icon: content["icon"].string,
order: Int(content["order"].stringValue),
aname: content["aname"].string,
options: Int(content["options"].stringValue),
items:content["items"].arrayObject)
subcategories.append(subcategory)
}
for subcategory in subcategories {
print(subcategory.name)
print(subcategory.desc)
print(subcategory.id)
}
print(subcategories.count)
我希望所有的子類別數據以及項目數據到我的模態類。如何實施?
,但如果我有這麼多的子類別,然後如何可以根據這裏計數 –
只有我發出JSON的樣本數據數量,我得到的所有子類別的數據。我的數據不僅僅是一個子類和一個項目。 –
你應該讓你的問題更清楚。 – Alexander