Coder Social home page Coder Social logo

swift-mini-json's Introduction

swift-mini-json

A single class compact (~ 40 loc) JSON reading library in swift. Meant to be dropped in to any project without fuzz. Most recent version was tested with swift 3.2 and 4.0. If you're stuck on an older version of swift, see the "legacy" folder for deprecated versions.

Properties

JSON:

{ name: 'Ford', wheelCount: 4, weight: 2125.5, model: { name: 'F5' } }

Swift:

let car = NSData(...) // E.g. result from API, file read etc.
let json = Json(data: car)
let carName = json["name"].string()
let wheelCount = json["wheelCount"].int()
let weight = json["weight"].float()
let nestedModelName = json["model"]["name"].string()

Arrays

JSON:

{ passengers: ['Kenny', 'Benny', 'Jenny', 'Lenny'] }

Swift:

for name in json["passengers"] {
    println(name.string()) 
}

JSON:

{ 
    components: {
        wheels: [ 
            { brand: 'BrandA', radius: 55.0, boltCount: 16 }, 
            { brand: 'BrandB', radius: 56.0, boltCount: 18 } ],
        engine: /* ... */
    }
}

Swift:

for wheel in json["components"]["wheels"] {
    let brand = wheel["brand"].string()
    let radius = wheel["radius"].float()
    let boltCount = wheel["boltCount"].int()
}

Missing properties and error handling

If a property is missing, the default value is returned:

let s = json["missing-string"].string() // => ""
let i = json["missing-int"].int() // => 0
let f = json["missing-float"].float() // => 0.0
let d = json["missing-double"].double() // => 0.0
for missing in json["missing"] { } // same as iterating over empty collection

The same applies for invalid type conversions:

let s = json["string"].int() // => 0
for item in json["string"] { } // same as iterating over empty collection

swift-mini-json's People

Contributors

erlandranvinge avatar

Watchers

 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.