我一直坚持Json解析。我检查了更多的例子。即-First Second Third Fourth您能帮助任何人吗?
我的模特在下面:
公共结构项{ 变量名称:字符串 var detail:字符串
public init(name: String, detail: String) {
self.name = name
self.detail = detail
}}
公共结构部分{ 变量名称:字符串 var个项目:[Item] var崩溃了:布尔
public init(name: String, items: [Item], collapsed: Bool = false) {
self.name = name
self.items = items
self.collapsed = collapsed
}}
和json解析
guard let path = Bundle.main.path(forResource: "AllMenu", ofType: "json") else { return }
let url = URL(fileURLWithPath: path)
do {
let data = try? Data(contentsOf: url)
let json = try? JSONSerialization.jsonObject(with: data!, options: .mutableContainers)
guard let menuDict = json as? Array<Any> else { return }
for arr in menuDict{
let sectionName = (arr as AnyObject).value(forKey: "name")
let cellName = (arr as? AnyObject)?.value(forKey: "sub_category")
let cellArray: Array = (cellName as? Array<Any>)!
var existingItems = data["items"] as? [[String: Any]] ?? [[String: Any]]()
for indexs in cellArray{
let CeName = (indexs as AnyObject).value(forKey: "name")
let displayName = (indexs as AnyObject).value(forKey: "display_name")
self.sections.append(index as! Section)
}
print(sectionName as Any)
print(cellName as Any)
}
print(" ")
}
json数据如下:
[{“ name”:“ rings”, “ sub_category”:[{
"name": "engagement",
"display_name": "Engagement"
},
{
"name": "band-couple band",
"display_name": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed scelerisque aliquet arcu, sed placerat turpis. Sed vulputate finibus nisi, nec gravida turpis consectetur quis. Nullam quis vestibulum ex. Integer lacinia quam sed rutrum tempus. Sed quis metus mollis, euismod ipsum in, vulputate turpis. Fusce suscipit ligula in efficitur interdum. Sed ante velit, vulputate nec nibh commodo, tempus molestie eros. Etiam consequat enim nisi, a mollis nisi gravida et. Fusce scelerisque ex vitae turpis fermentum facilisis."
},
{
"name": "Navaratnam+Collection",
"display_name": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed scelerisque aliquet arcu, sed placerat turpis."
},
{
"name": "cocktail",
"display_name": "Lorem ipsum dolor sit amet"
},
{
"name": "below+15000",
"display_name": "Under 15,000/-"
},
{
"name": "hearts",
"display_name": "Heart Rings"
},
{
"name": "halo",
"display_name": "Halo Rings"
},
{
"name": "view all",
"display_name": "View All"
}
]}]