Swift 之 JSONEncoder 和 JSONDecoder

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遵循了 Decodable 和 Encodable

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

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Morris_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值