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

Get key of dictionary on JSON parse with SwiftyJSON

我想在这个JSON

上得到"key of dictionary"(我是这么叫的,不知道是不是对的名字)
{
  "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 和 Swifty 创建了引擎JSON,它将通过完成处理程序在我的控制器中调用

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

我的问题是,如何将我的 peoplesjsonRecieve["People"] 映射到我的结构中? 我希望 "People with nice hair" 在我的 People 结构上作为 peopleLooks 的值。我认为 "People with nice hair" 是某种字典的关键或其他东西,但我不知道如何得到它。

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

当你遍历字典时,例如

for peeps in peoples

您可以使用

访问密钥
peeps.0

的值
peeps.1

可以使用键值循环。

for (key,subJson):(String, JSON) in json["People"] {
   // you can use key and subJson here.
}