To run the example project, clone the repo, and run pod install from the Example directory first.
You may find an example project here. It uses JustJSON to parse json data to data model.
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapodsTo integrate JustJson into your Xcode project using CocoaPods, specify it in your Podfile:
use_frameworks!
pod 'JustJson'Then, run the following command:
$ pod installlet dict = jsonStr.toDictionary()let jsonString = dict.toJSONStr()let foo = dict[keyPath: "first.second.abc 123"] //return an optional Anylet foo = dict[string: "first.second.abc 123"] //return an optional Stringlet foo = dict[dict: "first.second.abc 123"] //return an optional Dictionartlet foo = dict[array: "first.second.abc 123"] //return an optional Arrayfor foo in dict[arrayValue: "first.second.foos"] {
print(foo)
}Here comes a Dictionary:
userGroup = ["users": [
[
"id" : "1",
"name" : "Apple",
"age" : 18
],
[
"id" : "2",
"name" : "Boy",
"age" : 19
],
[
"id" : "3",
"name" : "Cat",
"age" : 30
],
]
]A User model:
struct User: JJMappable {
var id: String?
var name: String?
var age: Int
init(map: JJMapper) {
id = map[string: "id"]
name = map[string: "name"]
age = map[intValue: "age"]
}
}A UserGroup model:
struct UserGroup: JJMappable {
var users: [User]?
init(map: JJMapper) {
users = User.from(map[arrayValue: "users"])
}
}let data = userGroup[arrayValue: "users"][1] as! [String: Any]
let user: User? = User.from(data)let users: [Users]? = User.from(userGroup[arrayValue: "users"])let _userGroup = UserGroup.from(userGroup)Horst Leung
Ole Begemann [https://oleb.net/blog/2017/01/dictionary-key-paths/]
JustJson is available under the MIT license. See the LICENSE file for more info.