Giter Site home page Giter Site logo

json's Introduction

JSON

Version License Platform

This library helps you dealing with NSJSONSerialization. It has simple methods to convert NSData into a JSON object and to get a JSON object from a file.

JSON Object from NSData

If converting the NSData to JSON fails, you'll get a ParsingError.Failed error.

guard let url = URL(string: "http://httpbin.org/get") else { return }
let request = URLRequest(url: url)
URLSession.shared.dataTask(with: request) { data, _, error in
    do {
        let json = try data?.toJSON() as? [String : Any]
        print(json)
    } catch {
        // Handle error
    }

    expectation.fulfill()
}.resume()
{
  "args": {}, 
  "headers": {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp;q=0.8", 
    "Accept-Encoding": "gzip, deflate, sdch", 
    "Accept-Language": "en-US,en;q=0.8,es;q=0.6,nb;q=0.4", 
    "Host": "httpbin.org", 
    "Upgrade-Insecure-Requests": "1", 
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.71 Safari/537.36"
  }, 
  "origin": "000.00.000.000", 
  "url": "http://httpbin.org/get"
}
*/

JSON Object from file

If parsing the JSON fails, you'll get a ParsingError.Failed error. If the file is not found you'll get a ParsingError.NotFound error.

For files in the main bundle:

import JSON

var users: Any?
do {
    users = try JSON.from("users.json")
} catch {
    // Handle error
}

For files in in other bundles (for example Tests file bundle):

import JSON

var users: Any?
do {
    users = try JSON.from("users.json", bundle: Bundle(for: Tests.self))
} catch {
    // Handle error
}

Installation

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

pod 'JSON'

Author

Elvis Nuñez, @3lvis

License

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

json's People

Contributors

3lvis avatar nselvis avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

hyperoslo

json's Issues

Use immutable parsing options

Mutability is bad, it caused an error. [mutableCopy] does shallow coping, so I would prefer to use immutable type in json.

 id result = [NSJSONSerialization JSONObjectWithData:data
                                                options:0
                                                  error:&error];

Improve code example

In the Readme file -
expectation.fulfill() - remove it, not related to the json library. Makes harder to get familiar with code :)

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.