我想「詞典的鑰匙」(這就是我所謂的,不知道這是否是正確的名稱)這個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!將在幾分鐘內批准此答案。 –