4

Can somebody help with parsing this kind of JSON object?

{  
   "array":[  
      {  
         "title":"",
         "desc":""
      },
      {  
         "title":"",
         "desc":""
      },
      {  
         "title":"",
         "desc":""
      }
   ]
}

My code doesnt work

let task = self.session.dataTask(with: url) {
        data, response, error in
        if let data = data,
            let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] {
            for case let announcment in json!["array"]{
                guard let title = announcment["title"] as? String,
                    let description = announcment["desc"] as? String,
                else{ return }
    }
    task.resume()

Thank in advance for any help!

2
  • what issue are you getting Commented Sep 17, 2018 at 17:14
  • You should use Codable for Swift 4 and please explain what 'my code doesn't work' means, do you get an error? have you stepped through the code? Commented Sep 18, 2018 at 9:00

2 Answers 2

3

Pretty-printing your JSON makes it easier to work through:

{
   "array":[
      {
         "title":"",
         "desc":""
      },
      {
         "title":"",
         "desc":""
      },
      {
         "title":"",
         "desc":""
      }
   ]
}

You need to get the array first. An array of dictionaries is of type [[String: Any]].

let task = self.session.dataTask(with: url) {
    data, response, error in
    if let data = data,
        let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] {
            guard let announcements = json["array"] as? [[String: Any]] 
                else { return }

            announcements.forEach { announcement in
            guard let title = announcement["title"] as? String,
                let description = announcement["desc"] as? String
                else { return }
            // Do something with the result
            }
       }
  }
task.resume()
Sign up to request clarification or add additional context in comments.

Comments

1

You can structure your data and make it Codable:

struct Root: Codable {
    let array: [Announcement]
}
struct Announcement: Codable {
    let title: String
    let desc: String
}

let data = Data("""
{"array":[{"title":"","desc":""},{"title":"","desc":""},{"title":"","desc":""}]}
""".utf8)

do {
    let announcements = try JSONDecoder().decode(Root.self, from: data).array
    for announcement in announcements {
        print(announcement)
    }
} catch {
    print(error)
}

This will print

Announcement(title: "", desc: "")
Announcement(title: "", desc: "")
Announcement(title: "", desc: "")

1 Comment

Thanks for the answer. I didn't know about Cadable :)

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.