我想“词典的钥匙”(这就是我所谓的,不知道这是否是正确的名称)这个JSON获取字典的关键在JSON解析与SwiftyJSON
{
"People": {
"People with nice hair": {
"name": "Peter John",
"age": 12,
"books": [
{
"title": "Breaking Bad",
"release": "2011"
},
{
"title": "Twilight",
"release": "2012"
},
{
"title": "Gone Wild",
"release": "2013"
}
]
},
"People with jacket": {
"name": "Jason Bourne",
"age": 15,
"books": [
{
"title": "Breaking Bad",
"release": "2011"
},
{
"title": "Twilight",
"release": "2012"
},
{
"title": "Gone Wild",
"release": "2013"
}
]
}
}
}
首先,我已经创建了我的人员结构,将用于从这些JSON映射。 这里是我的人结构
struct People {
var peopleLooks:String?
var name:String?
var books = [Book]()
}
这里是我的书结构
struct Book {
var title:String?
var release:String?
}
从这个JSON,我创建了Alamofire和SwiftyJSON引擎,会在我的控制器通过完成处理程序被称为
Alamofire.request(request).responseJSON { response in
if response.result.error == nil {
let json = JSON(response.result.value!)
success(json)
}
}
这里是我在我的控制器中做的事
Engine.instance.getPeople(request, success:(JSON?)->void),
success:{ (json) in
// getting all the json object
let jsonRecieve = JSON((json?.dictionaryObject)!)
// get list of people
let peoples = jsonRecieve["People"]
// from here, we try to map people into our struct that I don't know how.
}
我的问题是,如何将我的peoples
从jsonRecieve["People"]
映射到我的结构? 我想要"People with nice hair"
作为peopleLooks
的值在我的People
结构。我认为"People with nice hair"
是字典或其他东西的关键,但我不知道如何得到它。
任何帮助,将不胜感激。谢谢!
Yeaaaahh,你救了我的一天!谢谢soooo,@NickCatib!将在几分钟内批准此答案。 –