Coder Social home page Coder Social logo

pppqppqpphejj / swiftjsonparser Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mrap/swiftjsonparser

0.0 2.0 0.0 39 KB

Parse JSON like a badass

Home Page: https://github.com/mrap/SwiftJSONParser

License: MIT License

Swift 95.83% Objective-C 4.17%

swiftjsonparser's Introduction

Parse JSON like a badass

Full disclaimer regarding performance

For production apps (or if you care about efficiency at all), I highly recommend using SwiftyJSON over this library. After benchmarking, I found that SwiftJSONParser could be up to 7x slower than SwiftyJSON.

This sucks

let jsonData = NSData(contentsOfURL: NSURL(string: url))
var jsonErrorOptional: NSError?
let jsonOptional: AnyObject! = NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions(0), error: &jsonErrorOptional)

if let json = jsonOptional as? Dictionary<String, AnyObject> {
    if let other = json["other"] as? Dictionary<String, AnyObject> {
        if let nicknames = other["nicknames"] as? Array<String> {
            if let handle = nicknames[0] as AnyObject? as? String {
                println("Some folks call me \(handle)")
            }
        }
    }
}

This is readable

let jsonData = NSData(contentsOfURL: NSURL(string: url))
let parser = JSONParser(jsonData)

if let handle = parser.getString("other.nicknames[0]") {
    println("Some folks call me \(handle)")
}

Usage

Sample JSON payload we want to parse

{
    "name": "Mike",
    "favorite_number": 19,
    "gpa": 2.6,
    "favorite_things": ["Github", 42, 98.6],
    "other": {
        "city": "San Francisco",
        "commit_count": 9000,
        "nicknames": ["mrap", "Mikee"]
    }
}

Get values of a specific type. Returns optionals

if let name = parser.getString("name") {
    println("My name is \(name)")
}

if let number = parser.getInt("favorite_number") {
    println("\(number) is my favorite number!")
}

if let gpa = parser.getDouble("gpa") {
    println("My stellar highschool gpa was \(gpa)")
}

Or get AnyObject if you're not sure

if let city = parser.get("other.city") {
    // city will be type AnyObject
}

Get an Array of values

if let favorites = parser.getArray("favorite_things") {
    // favorites => ["Github", 42, 98.6]
}

Error Handling

If you're not sure about incoming json data, check it first

let badJsonData = NSData(contentsOfURL: NSURL(string: url))
let parser = JSONParser(badJsonData)

// Check for an error
if parser.error != nil {
   // Do stuff with json
} else {
   // Handle the error
   println(parser.error)
}

Installation

The best way to use SwiftJSONParser is to import it as a framework.

  1. Clone a local copy of SwiftJSONParser in any directory you'd like. git clone [email protected]:mrap/SwiftJSONParser.git

  2. Drag SwiftJSONParser.xcodeproj into your project's project navigator

  3. Add SwiftJSONParser as a Target Dependency.

  • Go to your App Target > Build Phases
  • In Target Dependencies, press +, select SwiftJSONParser

swiftjsonparser's People

Contributors

mrap avatar smoothmaverick avatar

Watchers

James Cloos avatar developer_rui 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.