JSONUtilities

Build Status codecov.io Carthage Compatible Swift Version

轻松加载 JSON 对象并将其解码为结构体或类。json(atKeyPath:) 函数会根据常量或变量定义推断类型来进行解码,这意味着无需进行类型转换。解码 JSON 时,支持字符串键和键路径(键之间用点 . 分隔)。

安装

CocoaPods

pod 'JSONUtilities' 行添加到您的 Podfile

Carthage

github "lucianomarisi/JSONUtilities" 行添加到您的 Cartfile

手动

Sources 文件夹中的文件添加到您的 Xcode 项目。

Swift Package Manager

.Package(url: "https://github.com/lucianomarisi/JSONUtilities", majorVersion: 3) 行添加到您的 Package.swift

支持的类型

JSON 原始类型

JSON 原始类型的数组

自定义 JSON 对象和自定义 JSON 对象数组

例如,如果 MyClassMyStruct 符合 JSONObjectConvertible 协议

具有 String 键的类型化字典

InvalidItemBehaviour

在解码数组或字典时,可以传递 invalidItemBehaviour 参数,以控制在解码子项时发生错误时应采取的操作

JSON 加载示例

从文件

let filename = "myjsonfile"
let dictionary: [String: AnyObject] = try JSONDictionary.from(filename: filename)

从数据

let data: Data = ...
let dictionary: [String: AnyObject] = try JSONDictionary.from(jsonData: data)

JSON 解码示例

考虑一个表示人的 JSON 对象

{
  "name" : "John Doe",
  "age" : 24,
  "weight" : 72.4
}

内联解码 JSON

let jsonDictionary = try JSONDictionary.from(filename: "person.json")
let name: String = try jsonDictionary.json(atKeyPath: "name")
let age: Int = try jsonDictionary.json(atKeyPath: "age")
let weight: Int = try jsonDictionary.json(atKeyPath: "weight")
let profession: String? = jsonDictionary.json(atKeyPath: "profession") // Optional decoding

解码结构体或类

struct Person { //OR class Person {

  let name: String
  let age: Int
  let weight: Double
  let profession: String?

  init(jsonDictionary: JSONDictionary) throws {
    name = try jsonDictionary.json(atKeyPath: "name")
    age = try jsonDictionary.json(atKeyPath: "age")
    weight = try jsonDictionary.json(atKeyPath: "weight")
    profession = jsonDictionary.json(atKeyPath: "profession")
  }

}

通过符合 JSONObjectConvertible 协议解码嵌套的结构体或类

考虑一个公司 JSON 对象

{
    "name" : "Working name LTD.",
    "employees": [
        {
            "name": "John Doe",
            "age": 24,
            "weight": 72.4
        },
        {
            "name": "Jane Doe",
            "age": 22,
            "weight": 70.1
        }
    ]
}

通过使 Person 符合 JSONObjectConvertible 协议,Company 结构体可以解码 Person 结构体/类的数组

struct Company {
  let name: String
  let employees: [Person]

  init(jsonDictionary: JSONDictionary) throws {
    name = try jsonDictionary.json(atKeyPath: "name")
    employees = try jsonDictionary.json(atKeyPath: "employees")
  }
}

通过符合 JSONPrimitiveConvertible 来支持自定义原始类型

任何类型都可以扩展 JSONPrimitiveConvertible 协议以允许解码。 例如,扩展 URL请注意,此扩展是开箱即用的

extension URL: JSONPrimitiveConvertible {

  public typealias JSONType = String

  public static func from(jsonValue: String) -> Self? {
    return self.init(string: jsonValue)
  }

}

let urlDictionary = ["url": "www.google.com"]
let url: URL = try! urlDictionary.json(atKeyPath: "url") // www.google.com

也可以拥有 JSONPrimitiveConvertible 值的数组,例如

let urlsDictionary = ["urls": ["www.google.com", "www.yahoo.com"]]
let urls: [URL] = try! urlsDictionary.json(atKeyPath: "urls") // [www.google.com, www.yahoo.com]