Coder Social home page Coder Social logo

jason's Introduction

NOTE: JaSON has evolved into the far superior Marshal Go use that instead.

JaSON

Dead simple and safe JSON value extraction

Basic Usage

var json: JSONObject = ["url": "http://apple.com", "foo": (2 as NSNumber), "str": "Hello, World!", "array": [1,2,3,4,7], "object": ["foo": (3 as NSNumber), "str": "Hello, World!"], "bool": (true as NSNumber), "urls": ["http://apple.com", "http://google.com"], "user": ["name": "Jason", "email": "[email protected]"], "users": [["name": "Jason", "email": "[email protected]"], ["name": "Bob", "email": "[email protected]"]]]
do {
    var str: String = try json.valueForKey("str")
    var foo2: Int = try json.valueForKey("foo")
    var foo3: Int? = try json.valueForKey("foo")
    var foo4: Int? = try json.valueForKey("bar")
    var arr: [Int] = try json.valueForKey("array")
    var obj: JSONObject? = try json.valueForKey("object")
    let innerfoo: Int = try obj!.valueForKey("foo")
    let innerfoo2: Int = try json.valueForKey("object.foo")
    let bool: Bool = try json.valueForKey("bool")
    let url: NSURL = try json.valueForKey("url")
    let urls: [NSURL] = try json.valueForKey("urls")
}
catch {
    print("\(error)")
}

Resources

Check out my series of articles on JSON value extraction at my blog

And don't forget if you don't like do/catch you can always use try?... but you will lose error information.

Init Your Own Custom Classes

Want to init your own classes from JSONObjects or arrays of JSONObject?

struct User : JSONObjectConvertible {
    let name: String
    let email: String
    
    init(json: JSONObject) throws {
        name = try json.valueForKey("name")
        email = try json.valueForKey("email")
    }
}

do {
    let user: User = try json.valueForKey("user")
    let users: [User] = try json.valueForKey("users")
}
catch {
    print("\(error)")
}

jason's People

Contributors

jarsen avatar

Stargazers

Parker Rushton avatar Jeremy Stone avatar

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.