测试已测试 | ✗ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布最新版本 | 2017年11月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 Robin Oster 维护。
依赖项 | |
Alamofire | ~> 4.0 |
SwiftyJSON | ~> 3.0.0 |
是一个非常轻量级且简单的 JSON 到对象的映射器。您可以在 JSON 文件中直接定义您的访问属性定义您的类结构。整个 JSON 文件的解析由库 SwiftyJSON 完成 https://github.com/SwiftyJSON/SwiftyJSON.
RONetworking 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod "RONetworking"
示例 Employees.json
[
{
"firstName": "John",
"lastName": "Doe",
"age": 26
},
{
"firstName": "Anna",
"lastName": "Smith",
"age": 30
},
{
"firstName": "Peter",
"lastName": "Jones",
"age": 45
}
]
下一步,您必须创建您的数据模型(EmplyoeeContainer 和 Employee)。
Employee.swift
class Employee : ROJSONObject {
required init() {
super.init();
}
required init(jsonData:AnyObject) {
super.init(jsonData: jsonData)
}
required init(jsonString: String) {
super.init(jsonString:jsonString)
}
var firstname:String {
return Value<String>.get(self, key: "firstName")
}
var lastname:String {
return Value<String>.get(self, key: "lastName")
}
var age:Int {
return Value<Int>.get(self, key: "age")
}
}
然后实际上从 JSON 响应映射对象,您只需将数据传递给 Employee 类构造函数中的参数。它会自动创建您的数据模型。
var urlToJSON = "http://prine.ch/employeesWithout.json"
baseWebservice.getArray(urlToJSON, callback: { (status, employees:Array<Employee>) in
for employee in employees {
print("Firstname with Array: \(employee.firstname)")
}
})
或者您想将其存储在容器中
EmployeeContainer.swift
class EmployeeContainer : ROJSONObject {
required init() {
super.init();
}
required init(jsonData:AnyObject) {
super.init(jsonData: jsonData)
}
required init(jsonString: String) {
super.init(jsonString:jsonString)
}
lazy var employees:[Employee] = {
return Value<[Employee]>.getArray(self) as [Employee]
}()
}
var baseWebservice:BaseWebservice = BaseWebservice();
var urlToJSON = "http://prine.ch/employeesWithout.json"
baseWebservice.get(urlToJSON, callback: { (status, employeeContainer:EmployeeContainer) -> () in
println(employeeContainer.employees[0].firstname)
println(employeeContainer.employees[0].lastname)
println("Firstname: " + employeeContainer.employees[0].firstname)
println("Lastname: " + employeeContainer.employees[0].firstname)
println("Age: \(employeeContainer.employees[0].age)")
})
控制台输出如下所示
John
Doe
Firstname: John
Lastname: John
Age: 26
The MIT License (MIT)
Copyright (c) 2016 Robin Oster (http://prine.ch)
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Robin Oster,[email protected]