CrispJSON 允许您在使用 Swift 创建简洁且可读的 JSON 解析代码。可扩展且轻量级,它可以解析简单和复杂的 JSON。
平台 | OSX、iOS、watchOS、tvOS 和 Linux 软件包 |
---|---|
语言 | Swift 3.0 |
来源 | Originware |
Git 仓库 | bitbucket.org/originware/crispjson |
要求 | Xcode 8+ 或 Linux 的 Swift 3 编译工具链。 |
let dataSource = CrispJSON.JDataSource(jsonFromGooglePlaces)
let locations = CrispJSON.JParser(dataSource!).parse({ (json) in
// Create the MyLocation-s array (where MyLocation is a struct representing a location).
var results = [MyLocation]()
json["results"].forArray({ (result) in
guard let name = result ->> JValue<String>.value("name") else { return }
guard let address = result ->> JValue<String>.value("formatted_address") else { return }
let types = result ->> JValue<[String]>.value("types") // location types
// are optional.
let coordinate = result ->> "geometry" ->> "location"
guard let lat = coordinate ->> JValue<Double>.value("lat") else { return }
guard let lng = coordinate ->> JValue<Double>.value("lng") else { return }
let location = MyLocation(name, address, types, lat, lng)
results.append(location)
})
return results
})
以下为解析简化版 Google 地点 JSON
{
"results" : [
{
"name" : "Acme Anvil and Dynamite Company",
"formatted_address" : "Burbank, California",
"types" : [
"store",
"home_goods_store",
"funeral_home",
],
"geometry" : {
"location" : {
"lat" : 34.1808,
"lng" : -118.3090
},
}
}
}
CrispJSON 使用各种解析机制
->> 运算符作用于
迭代器:
下标:
有关广泛的用例,包括迭代和递归、对象数据类型检查、使用自定义解析闭包的复杂/重复解析、错误处理(简单的错误值传递和异常处理)、支持的数据源(流和非流)、自定义统计汇总和解析操作的调试跟踪,请参阅 git 仓库中的Playground 源代码。
直接集成 CrispJSON 源:
Add the two files under the Sources directory to your source (files: CrispJSON.swift and CrispJSON-support.swift)
Cocoapods:
Pod 名称是 “CrispJSON”
Swift Package:
cd CrispJSON swift build
这些在CrispJSON 问题 页面处理。
源代码包需要根据Apache 2.0授权提供。
请将评论和反馈发送至Terry Stillone @ Originware.com