如何使用SwiftyJSON获取此subJson?

时间:2016-05-18 16:19:29

标签: json swift swifty-json

"groups" : {
    "232" : {
      "name" : "John",
      "age" : "22"
      }
    },
    "143" : {
      "name" : "Dylan",
      "age" : "22"
      }
    }

如何获得" name"使用swiftyJSON?我得到了id值,但["名称"]总是没有这个代码:

        for (results,subJson):(String, JSON) in json["groups"] {
                    let id = results
                    print(id) //--> 232
                    let name = subJson["\(id)"]["name"].string
                    print(name) //--> nil
         }

1 个答案:

答案 0 :(得分:1)

name密钥位于subJson内,删除了id下标:

for (id, subJson):(String, JSON) in json["groups"] {
    print(id)
    if let name = subJson["name"].string {
        print(name)
    }
}