Aldo Lazuardi
Aldo Lazuardi

Reputation: 1948

How to get this subJson using SwiftyJSON?

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

How to get "name" using swiftyJSON? I got the id value but ["name"]'s always nil with this code:

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

Upvotes: 1

Views: 152

Answers (1)

Eric Aya
Eric Aya

Reputation: 70098

The name key is inside subJson, remove the id subscript:

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

Upvotes: 1

Related Questions