gpt4 book ai didi

ios - 使用 SwiftyJSON 获取 JSON 解析字典的键

转载 作者:可可西里 更新时间:2023-11-01 00:51:02 26 4
gpt4 key购买 nike

我想在这个 JSON 上获取“字典的键”(我就是这么叫的,不确定它是否正确)

{
"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"
}
]
}
}
}

首先,我已经创建了我的 People 结构,它将用于从这些 JSON 进行映射。这是我的人员结构

struct People {
var peopleLooks:String?
var name:String?
var books = [Book]()
}

这是我的 Book 结构

struct Book {
var title:String?
var release:String?
}

根据该 JSON,我使用 Alamofire 和 SwiftyJSON 创建了引擎,它们将通过完成处理程序在我的 Controller 中调用

Alamofire.request(request).responseJSON { response in
if response.result.error == nil {
let json = JSON(response.result.value!)
success(json)
}
}

这是我在 Controller 中所做的

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.
}

我的问题是,如何将 jsonRecieve["People"] 中的 peoples 映射到我的结构中?我希望 "People with nice hair" 作为我的 People 结构上的 peopleLooks 的值。我认为 "People with nice hair" 是字典的关键字之类的东西,但我不知道如何获得它。

如有任何帮助,我们将不胜感激。谢谢!

最佳答案

例如,当您遍历字典时

for peeps in peoples

您可以访问 key

peeps.0

和值(value)

peeps.1

关于ios - 使用 SwiftyJSON 获取 JSON 解析字典的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38951628/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com