0

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?

5
  • groups corresponds to a string, and not a JSON array. (because its enclosed in quotes) Extract the string, and then parse it as an array. Commented Jan 27, 2018 at 20:35
  • That's exactly what I needed to know, thanks Commented Jan 27, 2018 at 20:53
  • 2
    Just so you know, if you control the backend or the service in anyway or can approach the person responsible for it; ask them to not send it as a string. It breaks all conventions. Commented Jan 27, 2018 at 21:04
  • I actually do have access to the backend but I didn't write it myself and it's in rails so I wouldn't know how to go about changing it. If i were to change it would I send everything inside the string as nested responses instead? Commented Jan 27, 2018 at 21:44
  • yup, that is correct. Commented Jan 28, 2018 at 6:04

1 Answer 1

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

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

then parse groups with jsonSerialization

and

let id  = data["id"]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.