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

Son*_*ter 3 ios swift alamofire swifty-json

我想在这个 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"
        }
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

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

struct People {
    var peopleLooks:String?
    var name:String?
    var books = [Book]()
}
Run Code Online (Sandbox Code Playgroud)

这是我的 Book 结构

struct Book {
    var title:String?
    var release:String?
}
Run Code Online (Sandbox Code Playgroud)

从那个 JSON,我用 Alamofire 和 SwiftyJSON 创建了引擎,这些引擎将通过完成处理程序在我的控制器中调用

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

这是我在控制器中所做的

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.
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,如何将我的peoplesfrom映射jsonRecieve["People"]到我的结构中?我想"People with nice hair"作为peopleLooksPeople结构上的值。我以为"People with nice hair"是字典的钥匙什么的,但我不知道如何得到它。

任何帮助,将不胜感激。谢谢!

Mik*_*ash 5

例如,当您遍历字典时

for peeps in peoples
Run Code Online (Sandbox Code Playgroud)

您可以访问密钥

peeps.0
Run Code Online (Sandbox Code Playgroud)

和价值

peeps.1
Run Code Online (Sandbox Code Playgroud)