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 开发者 Swapcard
特性
- 非常快 -
benchmarks
- 全面测试
- 全面文档
- 干净代码
- 美丽的 API
- 定期更新
- 支持 iOS、OSX、tvOS、watchOS
- 兼容 Carthage / CocoaPods
- 提供扩展 -
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
请参阅参考部分以获取属性的全列表。
JSONKey
这个想法来自
SwiftyUserDefaults
,由 Radek Pietruszewski (GitHub, Twitter, 博客) 创造。
> 我强烈建议您阅读他的相关文章!
按照下列方式定义和使用您的 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
类型的完整列表。
第三方库
- DroidsOnRoids/Moya-JASON 为 Moya 提供的 JASON 绑定。
示例
此示例使用了 Dribbble API (文档)。
> 服务器响应的示例可以在 Tests/Supporting Files/shots.json 中找到
- 步骤 1: 将
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")
}
- 步骤 2: 创建
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]!
}
}
- 步骤 3: 使用
JASON+Alamofire.swift
扩展来获取 shots
Alamofire.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() |
如果需要,配置 JSON.dateFormatter 以进行
nsDate
解析
安装
Carthage
Carthage 是一个去中心化的依赖管理器,它自动处理将框架添加到您的 Cocoa 应用程序的过程。
您可以使用以下命令使用 Homebrew 安装 Carthage
$ brew update
$ brew install carthage
要在 Xcode 项目中使用 Carthage 集成 JASON
,请在您的 Cartfile
中指定它
github "delba/JASON" >= 3.0
CocoaPods
CocoaPods 是 Cocoa 项目的依赖项管理器。
您可以使用以下命令安装它
$ gem install cocoapods
要使用 CocoaPods 将 JASON
集成到您的 Xcode 项目中,在您的 Podfile
中指定它
use_frameworks!
pod 'JASON', '~> 3.0'
许可
版权所有(c)2015-2019 Damien(《http://delba.io》)
在此特此许可,免费提供给任何获取此软件及其相关文档文件(以下简称“软件”)副本的个人使用软件不受任何限制,包括但不限于使用、复制、修改、合并、发布、分发、转授许可和/或销售软件副本的权利,以及允许向软件提供的人士进行操作,但subject to the following conditions
上述版权声明和本许可声明应包含在软件的任何副本或实质性部分中。
软件按“原样”提供,不受任何明示或暗示的保证,包括但不限于适销性、特定用途适用性和非侵权性。在任何事件中,作者或版权所有者不应对任何索赔、损害或其他责任承担任何责任,无论这些责任是合同责任、侵权责任或其他性质的责任,是否源于、与或因软件或其他软件的使用、操作或其他使用有关。