Swift 之 JSONEncoder 和 JSONDecoder

摘自官方文档:

/// A type that can convert itself into and out of an external representation.
///
/// `Codable` is a type alias for the `Encodable` and `Decodable` protocols.
/// When you use `Codable` as a type or a generic constraint, it matches
/// any type that conforms to both protocols.
public typealias Codable = Decodable & Encodable

对象进行jsonEncode和jsonDecode

一个对象如果需要被编码/解码的话,该对象所属的类需要遵循 Decodable & Encodable 协议。

Player 这类为例,Player遵循了 DecodableEncodable

import Foundation

struct Player: Codable {
    var name: String
    var highScore: Int = 0
    var history: [Int] = []

    enum CodingKeys: String, CodingKey {
        case name = "Name"
        case highScore = "HighScore"
        case history = "History"
    }
    
    init(_ name: String) {
        self.name = name
    }
}

//Codable, Equatable
extension Player {
    mutating func updateScore(_ newScore: Int) {
        history.append(newScore)
        if highScore < newScore {
            print("\(newScore)! A new high score for \(name)! ")
            highScore = newScore
        }
    }
}

初始化一个Player对象,并对其进行编码和解码,看看编码和解码之后的数据

var jsonData: Data?

// MARK: encode(编码)

// Player对象
var player = Player("Tomas")
// 设置歌手分数
player.updateScore(50)
// 初始化一个encoder对象
let encoder = JSONEncoder()
do  {
    // 将player对象encod(编码)
    let data: Data = try encoder.encode(player)
    // 打印
    print(data)
    print(String(data: data, encoding: String.Encoding.utf8) as Any)
    print(player)
    
    jsonData = data
} catch {
    
}

// MARK: decode(解码)
let decoder = JSONDecoder()
do {
    // 解码得到player对象
    let player: Player = try decoder.decode(Player.self, from: jsonData!)
    // 打印
    print(player)
    print(player.name)
} catch {
    
}

一个对象被jsonEncode后,对象将被转成 Data 类型的数据。
再将对象的 Data 数据通过jsonDecode,可以还原原来的对象。

通过对对象的json编码和解码,有助于我们理解数据在计算机中的存储。Data 其本质就是二进制流。

JSON字符串转模型

这是一个字符串

let jsonString: String = """
{
    "name" : "Tomas",
    "highScore" : 50,
    "history" : [30, 40, 50]
}
"""

如何将其解析成player对象?

// 将JSON字符串转成 Data
let jsonData: Data = jsonString.data(using: String.Encoding.utf8)!
// 将 data 转成对象
let decoder = JSONDecoder()
do {
    // 解码得到player对象
    let player: Player = try decoder.decode(Player.self, from: jsonData)
    // 打印
    print(player)
    print(player.name)
} catch {

}

这种情况多用于客户端向服务端发送HTTP请求之后,解析返回数据,如果后台返回的是标准的JSON字符串的话直接这样解析就可以了。

JSONSerialization

Serialization 是序列化的意思,JSONSerialization 顾名思义是对JSON进行序列化。

JSONSerialization 是对 JSON 字符串进行序列化和反序列化的工具类。用这个类可以将JSON转成对象,也可以将对象转成JSON。

  • objc 转 json
let dict: [String: Any] = ["name" : "Tomas",
                           "highScore" : 50,
                           "history" : [30, 40, 50]
]
if JSONSerialization.isValidJSONObject(dict) == false {
    return
}
// 将objc转成data
let data: Data = try! JSONSerialization.data(withJSONObject: dict, options: .fragmentsAllowed)
// 将data转成字符串输出
let string = String(data:data, encoding: String.Encoding.utf8)
print(string as Any)

打印结果:

Optional("{\"history\":[30,40,50],\"name\":\"Tomas\",\"highScore\":50}")

这是一个标准的,带转义的JSON字符串。也就是说我们将字典转成了JSON字符串。

  • json 转 objc
let jsonString = "{\"history\":[30,40,50],\"name\":\"Tomas\",\"highScore\":50}"
let data = jsonString.data(using: String.Encoding.utf8)
let dict = try! JSONSerialization.jsonObject(with: data!, options: .allowFragments)
print(dict)

打印结果:

{
    highScore = 50;
    history =     (
        30,
        40,
        50
    );
    name = Tomas;
}

参考

[1] JSONEncoder文档:https://developer.apple.com/documentation/foundation/jsonencoder/
[2] JSONDecoder文档:https://developer.apple.com/documentation/foundation/jsondecoder
[3] JSONSerialization文档:https://developer.apple.com/documentation/foundation/jsonserialization

你可能感兴趣的:(Swift 之 JSONEncoder 和 JSONDecoder)