Latest 0.1.0
Homepage https://github.com/sergioestevao/SVEJSONObject
License MIT
Platforms ios 8.0, requires ARC
Authors

CI Status
Version
License
Platform

JSONObject is a simple JSON deserializer using Swift.

The implementation is minimal but very expressive easily extendable.
One of the main features is the handling and trackdown of errors in the json objects.

Usage

To read a json file like this

[
  {
    "id": 1,
    "name": "Sergio",
    "weight": 70.5,
    "photos": []
  },
  {
    "id": 2,
    "name": "Miguel",
    "weight": 65.5,
    "photos": [{
             "url": "http://fake.com/miguel.jpeg",
             "width": 100,
             "height": 200
             }]
  }
]

You simple need to do this

    guard let url = NSBundle.mainBundle().URLForResource("sample", withExtension: "json") else {
        print("Sample.json not found")
        return
    }

    do {
        let users = try JSONObject(url: url)
        for user in users {
            let id = try user["id"].int()
            let name = try user["name"].string()
            let weight = try user["weight"].double()
            print("(id) : (name)'s weight is (weight)")
        }
    } catch {
        print(error)
        return
    }

Initialization

JSONObject can be initialized with an instance of AnyObject?, NSData? or NSURL

let jsonFromAnyObject = try JSONObject(json:object) // where object is an instance of AnyObject?
let jsonFromNSData = try JSONObject(data:data) // where data is an instance of NSData
let jsonFromNSURL = try JSONObject(url: url) // where url is an instance of NSURL

Accessing JSON values

You can access JSON values by using subscripts.

Using subscript

Use a subscript with an integer or a string key depending on whether the JSON is an array or a dictionary.

let user = try users[0]
let name = try user["name"]

Chaining subscript calls

Each subscript call returns an instance of JASON.JSON to allow chaining.

let name = try users[0]["name"]

Iterating over a JSON array

If the underlying JSON object is an array, you can iterate over it.

for user in users {
    let name = try user["name"]
}

Acessing JSON values

After you interate to the right location you can use any of these functions to get the final object

  • dictionary()
  • array()
  • string()
  • int()
  • float()
  • double()
  • bool()
  • null()

JSONObject also provides an helper method to covert to NSDate

  • dateWithFormater(formmatter)

Error handling

It any of the keys doesn’t exist, an index is out of bounds or the type is incorrect JSONObject will trown a JSONValidationError
that shows the type of error and where it happened

do {
  let name = try users[0]["fullname"] // that's fine too
} catch {
  print(error)//Prints Root[0]: Missing "fullname" element in dictionary.
}

Example Project

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

Requirements

  • iOS 8 and above

Installation

SVEJSONObject is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "SVEJSONObject"

Author

Sérgio Estêvão, [email protected]

License

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

Latest podspec

{
    "name": "SVEJSONObject",
    "version": "0.1.0",
    "summary": "A simple JSON data deserializer using Swift.",
    "description": "JSONObject is a simple JSON deserializer using Swift.nnThe implementation is minimal but very expressive easily extendable.nOne of the main features is the handling and trackdown of errors in the json objects.",
    "homepage": "https://github.com/sergioestevao/SVEJSONObject",
    "license": "MIT",
    "authors": {
        "Seu0301rgio Esteu0302vau0303o": "[email protected]"
    },
    "source": {
        "git": "https://github.com/sergioestevao/SVEJSONObject.git",
        "tag": "0.1.0"
    },
    "social_media_url": "https://twitter.com/SergioEstevao",
    "platforms": {
        "ios": "8.0"
    },
    "requires_arc": true,
    "source_files": "Pod/Classes/**/*"
}

Pin It on Pinterest

Share This