CocoaPods trunk is moving to be read-only. Read more on the blog, there are 18 months to go.

JustJson 0.1.4

JustJson 0.1.4

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Feb 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Horst Leung.



JustJson 0.1.4

  • By
  • PROJECT_OWNER

JustJson

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Example

Convert to Dictionary from JSON string

let dict = jsonStr.toDictionary()

Convert Dictionary [String: Any] to JSON string

let jsonString = dict.toJSONStr()

Get Data from Dictionary

let foo = dict[keyPath: "first.second.abc 123"] //return an optional Any
let foo = dict[string: "first.second.abc 123"] //return an optional String
let foo = dict[dict: "first.second.abc 123"] //return an optional Dictionart
let foo = dict[array: "first.second.abc 123"] //return an optional Array

Looping

for foo in dict[arrayValue: "first.second.foos"] {
  print(foo)
}

Object Mapping

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"])
    }
}

If you want to map a user

let data = userGroup[arrayValue: "users"][1] as! [String: Any]
let user: User? = User.from(data)

If you want to get users

let users: [Users]? = User.from(userGroup[arrayValue: "users"])

Nested mapping

let _userGroup = UserGroup.from(userGroup)

More Exampes:

Test case

Author

Horst Leung

Thanks

Ole Begemann [https://oleb.net/blog/2017/01/dictionary-key-paths/]

License

JustJson is available under the MIT license. See the LICENSE file for more info.