Swift 4 游乐场 - 从 JSON 得到 object/result
Swift 4 Playground - getting object/result from JSON
我在 Playgrounds (MacOS) 中使用 Swift 4,作为初学者测试我的代码...我想从远程 [=19] 获得 object / title 的结果=].
代码一直工作到 'print(object.title)' 点,我希望它 return 导入的第一个标题的值 JSON。
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
// Create structer of Post
struct Post: Codable {
var userId: Int
var title: String
var body: String
}
// Remote JSON to Structed Object
let url = URL(string: "https://jsonplaceholder.typicode.com/posts")!
let jsonData = try! Data(contentsOf: url)
let datastring = String(data: jsonData, encoding: .utf8)
let decoder = JSONDecoder()
do {
// Decode data to object
let object = try decoder.decode(Post.self, from: jsonData)
print(object.title)
}
catch {
// Error Catch
//print(error)
}
请(学习)阅读 JSON。根对象是一个数组(用 []
表示)所以你需要解码 [Post]
和一个循环来打印所有项目:
let object = try decoder.decode([Post].self, from: jsonData)
for post in object {
print(post.title)
}
永远,永远,永远不要忽略错误
} catch {
print(error)
}
此外,请注意 Swift4 的所有功能。我的意思是 Swift 4 中的编码、解码和序列化。因此,您可以使用它。我已经为 Playground 添加了代码:
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
typealias JSONModel = [JSONModelElement]
class JSONModelElement: Codable {
let userID, id: Int?
let title, body: String?
enum CodingKeys: String, CodingKey {
case userID = "userId"
case id, title, body
}
}
let url = URL(string: "https://jsonplaceholder.typicode.com/posts")!
let jsonData = try! Data(contentsOf: url)
if let jsonModel = try? JSONDecoder().decode(JSONModel.self, from: jsonData) {
for element in jsonModel {
print(element.title)
}
}
编码愉快!
我在 Playgrounds (MacOS) 中使用 Swift 4,作为初学者测试我的代码...我想从远程 [=19] 获得 object / title 的结果=].
代码一直工作到 'print(object.title)' 点,我希望它 return 导入的第一个标题的值 JSON。
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
// Create structer of Post
struct Post: Codable {
var userId: Int
var title: String
var body: String
}
// Remote JSON to Structed Object
let url = URL(string: "https://jsonplaceholder.typicode.com/posts")!
let jsonData = try! Data(contentsOf: url)
let datastring = String(data: jsonData, encoding: .utf8)
let decoder = JSONDecoder()
do {
// Decode data to object
let object = try decoder.decode(Post.self, from: jsonData)
print(object.title)
}
catch {
// Error Catch
//print(error)
}
请(学习)阅读 JSON。根对象是一个数组(用 []
表示)所以你需要解码 [Post]
和一个循环来打印所有项目:
let object = try decoder.decode([Post].self, from: jsonData)
for post in object {
print(post.title)
}
永远,永远,永远不要忽略错误
} catch {
print(error)
}
此外,请注意 Swift4 的所有功能。我的意思是 Swift 4 中的编码、解码和序列化。因此,您可以使用它。我已经为 Playground 添加了代码:
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
typealias JSONModel = [JSONModelElement]
class JSONModelElement: Codable {
let userID, id: Int?
let title, body: String?
enum CodingKeys: String, CodingKey {
case userID = "userId"
case id, title, body
}
}
let url = URL(string: "https://jsonplaceholder.typicode.com/posts")!
let jsonData = try! Data(contentsOf: url)
if let jsonModel = try? JSONDecoder().decode(JSONModel.self, from: jsonData) {
for element in jsonModel {
print(element.title)
}
}
编码愉快!