JSON数据的解析和生成(Swift)

课题

  • 将 JSON 字符串反序列化为 Persons 类(结构)的对象 ,然后将这个对象序列化为 JSON 字符串。
  • Persons 类(结构)包含一个字段:Person 类(结构)的 persons 数组。
  • Person 类(结构)包含两个字段:字符串类型的 name 字段和整数类型的 age 字段。

Codable

public typealias Codable = Decodable & Encodable
public protocol Decodable {}
public protocol Encodable {}

Swift 对象与 JSON 数据相互转换的关键是实现 Codable 接口。

示例

import Foundation

struct Persons : Codable {
    let persons: [Person]
}

struct Person : Codable {
    let name: String
    let age: Int
    // only necessary when we want to customize the keys
    enum CodingKeys : String, CodingKey {
        case name = "name"
        case age = "age"
    }
}

let jsonString = """
{
  "persons" : [
    {
      "name" : "Joe",
      "age" : 12
    }
  ]
}
"""
let jsonData = jsonString.data(using: .utf8)!
let decoder = JSONDecoder()
let o = try! decoder.decode(Persons.self, from: jsonData)
print(o) // Persons(persons: [__lldb_expr_65.Person(name: "Joe", age: 12)])

let encoder = JSONEncoder()
//encoder.outputFormatting = .prettyPrinted
let data = try! encoder.encode(o)
print(String(data: data, encoding: .utf8)!) // {"persons":[{"name":"Joe","age":12}]}

参考链接

Ultimate Guide to JSON Parsing with Swift 4
http://www.jsoncafe.com/ 可以根据 JSON 数据自动生成实现 Codable 的 Swift 类。

原文地址:https://www.cnblogs.com/zwvista/p/9519776.html