Diese Präsentation wurde erfolgreich gemeldet.
Die SlideShare-Präsentation wird heruntergeladen. ×

Working with the Web: 
Decoding JSON

Weitere Verwandte Inhalte

Ähnliche Bücher

Kostenlos mit einer 30-tägigen Testversion von Scribd

Alle anzeigen

Ähnliche Hörbücher

Kostenlos mit einer 30-tägigen Testversion von Scribd

Alle anzeigen

Working with the Web: 
Decoding JSON

  1. 1. Unit 5—Lesson 5: Working with the Web: 
 Decoding JSON
  2. 2. JavaScript Object Notation JSON { "name": "Daren Estrada", "favorite_movie": { "title": "Finding Dory", "release_year": "2016" } } An open standard format that uses human readable text to transmit objects • Each object consists of attribute-value pairs Used primarily to transmit data between a server and applications Language-independent data format
  3. 3. { "name": "Daren Estrada", "favorite_movies": [ { "title": "Finding Dory", "release_year": 2016 }, { "title": "Inside Out", "release_year": 2015 } ] } The basics JSON
  4. 4. The basics JSON { "name": "Daren Estrada", "favorite_movies": [ { "title": "Finding Dory", "release_year": 2016 }, { "title": "Inside Out", "release_year": 2015 } ] }
  5. 5. The basics JSON { "name": "Daren Estrada", "favorite_movies": [ { "title": "Finding Dory", "release_year": 2016 }, { "title": "Inside Out", "release_year": 2015 } ] }
  6. 6. The basics JSON { "name": "Daren Estrada", "favorite_movies": [ { "title": "Finding Dory", "release_year": 2016 }, { "title": "Inside Out", "release_year": 2015 } ] }
  7. 7. Convert JSON data to Swift types let task = URLSession.shared.dataTask(with: url) { (data, response, error) in let jsonDecoder = JSONDecoder() if let data = data, let report = try? jsonDecoder.decode([String: String].self, from: data) { print(report) } } task.resume()
  8. 8. Decoding into custom model objects { "report_date": "2018-01-20", "profile_id": "136442", "name": "Final Results for Q4 2017", "read_count": "5" }
  9. 9. Decoding into custom model objects struct Report { let name: String let creationDate: Date let profileID: String let readCount: Int? }
  10. 10. Decoding into custom model objects struct Report: Codable { let name: String let creationDate: Date let profileID: String let readCount: Int? enum CodingKeys: String, CodingKey { case name case creationDate = "report_date" case profileID = "profile_id" case readCount = "read_count" } }
  11. 11. Decoding into custom model objects init(from decoder: Decoder) throws { let valueContainer = try decoder.container(keyedBy: CodingKeys.self) self.creationDate = try valueContainer.decode(String.self, forKey: CodingKeys.creationDate) self.profileID = try valueContainer.decode(URL.self, forKey: CodingKeys.profileID) self.readCount = try? valueContainer.decode(String.self, forKey: CodingKeys.readCount) }
  12. 12. Update the request completion handler let task = URLSession.shared.dataTask(with: url) { (data, response, error) in let jsonDecoder = JSONDecoder() if let data = data, let report = try? jsonDecoder.decode(Report.self, from: data) { print(report) } } task.resume()
  13. 13. Working with the Web: Decoding JSON Unit 5—Lesson 6 Learn how to read and write basic JSON. Learn how to convert JSON to and from Swift types and into your own custom model objects.
  14. 14. Lab: iTunes Search (Part 2) Unit 5—Lesson 4 Using the iTunes Search playground you created in the last lesson, serialize the retrieved data into a custom model object.
  15. 15. © 2017 Apple Inc. This work is licensed by Apple Inc. under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International license.

×