GithubHelp home page GithubHelp logo

fattomhk / justjson Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 31 KB

JustJson is a simple project for handling JSON with keypaths. You can map a dictionary to a model Object.

License: MIT License

Ruby 11.53% Swift 86.25% Objective-C 2.22%
swift json dictionary cocoapods json-string objectmapper

justjson's Introduction

JustJson

CI Status Version License Platform

Example

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

Full Example

You may find an example project here. It uses JustJSON to parse json data to data model.

Requirements

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate JustJson into your Xcode project using CocoaPods, specify it in your Podfile:

use_frameworks!

pod 'JustJson'

Then, run the following command:

$ pod install

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.

justjson's People

Contributors

horstleung avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

justjson's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.