JASON 是一个用 Swift 编写的 更快速的 JSON
反序列化器。
JASON is the best framework we found to manage JSON at Swapcard. This is by far the fastest and
the most convenient out there, it made our code clearer and improved the global performance
of the app when dealing with large amount of data.
Gautier Gédoux, Swapcard 的首席 iOS 开发者
benchmarks
Extensions/
let json = JSON(anything) // where `anything` is `AnyObject?`
如果您正在使用 Alamofire
, 将 JASON+Alamofire.swift
包含在您的项目中,以获得更棒的体验
Alamofire.request(.GET, peopleURL).responseJASON { response in
if let json = response.result.value {
let people = json.map(Person.init)
print("people: \(people)")
}
}
如果您正在使用 Moya
, 请查看 Moya-JASON
!
使用下标来解析 JSON
对象
json["people"][0]["name"]
// Or with a path:
json[path: "people", 0, "name"]
通过使用计算属性 json.<type>
将 JSON
值转换为其适当的类型
let name = json["name"].string // the name as String?
非可选变体 json.<type>Value
如果不存在/不可转换,将返回一个默认值
let name = json["wrong"].stringValue // the name will be ""
如果您想自己转换,您还可以将内部值作为 AnyObject?
访问
let something = json["something"].object
有关属性的完整列表,请参阅参考部分。
这个想法是从 Radek Pietruszewski 的
SwiftyUserDefaults
中借鉴的 (GitHub, Twitter, Blog).
定义和使用你的 JSONKey
如下
// With a int key:
let personKey = JSONKey<JSON>(0)
let personJSON = peopleJSON[personKey]
// With a string key:
let nameKey = JSONKey<String>("name")
let name = personJSON[nameKey]
// With a path:
let twitterURLKey = JSONKey<NSURL?>(path: 0, "twitter")
let twitterURL = peopleJSON[twitterURLKey]
您可能会发现扩展 JSONKeys
更方便,如示例部分所示。
有关 JSONKey
类型的完整列表,请参阅参考部分。
此示例使用 Dribbble API (文档)。
JSONKeys
以定义你的 JSONKey
JSON.dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
extension JSONKeys {
static let id = JSONKey<Int>("id")
static let createdAt = JSONKey<NSDate?>("created_at")
static let updatedAt = JSONKey<NSDate?>("updated_at")
static let title = JSONKey<String>("title")
static let normalImageURL = JSONKey<NSURL?>(path: "images", "normal")
static let hidpiImageURL = JSONKey<NSURL?>(path: "images", "hidpi")
static let user = JSONKey<JSON>("user")
static let name = JSONKey<String>("name")
}
Shot
和 User
模型struct Shot {
let id: Int
let title: String
let normalImageURL: NSURL
var hidpiImageURL: NSURL?
let createdAt: NSDate
let updatedAt: NSDate
let user: User
init(_ json: JSON) {
id = json[.id]
title = json[.title]
normalImageURL = json[.normalImageURL]!
hidpiImageURL = json[.hidpiImageURL]
createdAt = json[.createdAt]!
updatedAt = json[.updatedAt]!
user = User(json[.user])
}
}
struct User {
let id: Int
let name: String
let createdAt: NSDate
let updatedAt: NSDate
init(_ json: JSON) {
id = json[.id]
name = json[.name]
createdAt = json[.createdAt]!
updatedAt = json[.updatedAt]!
}
}
JASON+Alamofire.swift
扩展来获取 shotsAlamofire.request(.GET, shotsURL).responseJASON { response in
if let json = response.result.value {
let shots = json.map(Shot.init)
}
}
包含
JASON+Properties.swift
以获得更多类型!
属性 | JSONKey 类型 | 默认值 |
---|---|---|
string |
String? |
|
stringValue |
String |
"" |
int |
Int? |
|
intValue |
Int |
0 |
double |
Double? |
|
doubleValue |
Double |
0.0 |
float |
Float? |
|
floatValue |
Float |
0.0 |
nsNumber |
NSNumber? |
|
nsNumberValue |
NSNumber |
0 |
cgFloat |
CGFloat? |
|
cgFloatValue |
CGFloat |
0.0 |
bool |
Bool? |
|
boolValue |
Bool |
false |
nsDate |
NSDate? |
|
nsURL |
NSURL? |
|
dictionary |
[String: AnyObject]? |
|
dictionaryValue |
[String: AnyObject] |
[:] |
jsonDictionary |
[String: JSON]? |
|
jsonDictionaryValue |
[String: JSON] |
[:] |
nsDictionary |
NSDictionary? |
|
nsDictionaryValue |
NSDictionary |
NSDictionary() |
array |
[AnyObject]? |
|
arrayValue |
[AnyObject] |
[] |
jsonArray |
[JSON]? |
|
jsonArrayValue |
[JSON] |
[] |
nsArray |
NSArray? |
|
nsArrayValue |
NSArray |
NSArray() |
如果需要进行
nsDate
解析,请配置 JSON.dateFormatter
Carthage 是一个去中心化的依赖管理器,它可以自动将框架添加到您的 Cocoa 应用程序中。
您可以使用 Homebrew 通过以下命令安装 Carthage
$ brew update
$ brew install carthage
要使用 Carthage 将 JASON
集成到您的 Xcode 项目中,请在您的 Cartfile
中指定它
github "delba/JASON" >= 3.0
CocoaPods 是 Cocoa 项目的依赖管理器。
您可以使用以下命令安装它
$ gem install cocoapods
要使用 CocoaPods 将 JASON
集成到您的 Xcode 项目中,请在您的 Podfile
中指定它
use_frameworks!
pod 'JASON', '~> 3.0'
版权所有 (c) 2015-2019 Damien (http://delba.io)
特此授予任何人免费获得本软件及相关文档文件(以下简称“软件”)副本的许可,以处理本软件,不受限制,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本的权利,并允许向其提供软件的人员获得此项权利,但须遵守以下条件
上述版权声明和本许可声明应包含在所有副本或本软件的重要部分中。
本软件按“原样”提供,不提供任何形式的明示或暗示保证,包括但不限于适销性、适用于特定目的和不侵权的保证。在任何情况下,作者或版权持有者均不对任何索赔、损害或其他责任负责,无论是在合同诉讼、侵权行为或其他方面,因本软件或本软件的使用或其他处理而产生、源于或与之相关的任何索赔、损害或其他责任负责。