B-Brennan
B-Brennan

Reputation: 133

How to access specific data in a JSON response in Swift

I have this JSON response when I run my swift program.

{
"success": true,
"info": "Groups",
"data": {
    "groups": "[{\"id\":1,\"name\":\"test\",\"user_id\":1,\"active\":null,\"public\":true,\"image_file_name\":null,\"image_content_type\":null,\"image_file_size\":null,\"image_updated_at\":null,\"created_at\":\"2017-10-15T20:40:13.671+01:00\",\"updated_at\":\"2017-10-15T20:40:13.671+01:00\",\"hashtag\":null},{\"id\":4,\"name\":\"test_again\",\"user_id\":1,\"active\":null,\"public\":false,\"image_file_name\":null,\"image_content_type\":null,\"image_file_size\":null,\"image_updated_at\":null,\"created_at\":\"2018-01-16T18:17:06.575+00:00\",\"updated_at\":\"2018-01-16T18:17:06.575+00:00\",\"hashtag\":null}]"
}

However I want to access the information contained inside of "groups", specifically the id and name of the groups. Is there any easy way to do this?

Upvotes: 0

Views: 448

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100533

let dic  = response["data"] as! [String:Any]

let groups = dic["groups"] as! String

then parse groups with jsonSerialization

and

let id  = data["id"]

Upvotes: 1

Related Questions