JSONNeverDie is an auto reflection tool from JSON to Model, a user friendly JSON encoder / decoder, aims to never die.
set up a Model:
class People: JSONNDModel {
var name = ""
}
reflex JSON to Model automatic:
let json = JSONND(dictionary: ["name": "JohnLui"])
let people = People(JSONNDObject: json)
print(people.name) // get "JohnLui"
- JSON to Model reflection automatic
- auto reflection with no need of init()
- supports multi-level reflection
Read the documentation of auto reflection.
- supports all types: Int, Float, Bool, String, Array
- user friendly: Xcode can prompt all available types
- provides both Optional-type(Int?) and Original-type(Int)
And JSONNeverDie is well tested.
- iOS 7.0+
- Xcode 7
import JSONNeverDie
parse a JSON from network and get a string:
let data = NSData(contentsOfURL: NSURL(string: "http://httpbin.org/get?hello=world")!)!
let json = JSONND.initWithData(data)
print(json["args"]["hello"].stringValue)
// init from NSData
let data = NSData(contentsOfURL: NSURL(string: "http://httpbin.org/get?hello=world")!)!
let json = JSONND.initWithData(data)
// init from Array
let arrayJSON = JSONND(array: ["hello", "world", 100])
// init from Dictionary
let dictionaryJSON = JSONND(dictionary: ["hello": "world", "hey": "guys"])
let value = json["key"].int
let value = json["key"].intValue
let value = json["key"].string
let value = json["key"].stringValue
let value = json["key"].float
let value = json["key"].floatValue
let value = json["key"].bool
let value = json["key"].boolValue
let value = json["key"].array
let value = json["key"].arrayValue
// more than one level
let value = json["key"]["key1"]["key2"].int
if let jsonArray = json["array"].array {
for jsonItem in jsonArray {
let value = jsonItem["key"].int
}
}
// or just
let value = json["array"].arrayValue[0]["key"].int
let string = json.jsonString
Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate JSONNeverDie into your Xcode project using Carthage, specify it in your Cartfile:
github "JohnLui/JSONNeverDie"
Then fetch and build JSONNeverDie:
carthage update
At last, add it to "Embedded Binaries" in the general panel use the "Add Other..." button. The JSONNeverDie.framework binary file is lying in ./Carthage/Build/iOS
directory.
git submodule add https://github.com/johnlui/JSONNeverDie.git
open JSONNeverDie
then drag JSONNeverDie.xcodeproj to your Project, that's it!
If you want to run your project on device with JSONNeverDie, just go to PROJECT->TARGETS->[your project name]->General->Embedded Binaries, click +, select JSONNeverDie.frameWork and click "Add".
Clone all files in the Source
directory into your project.
##Contribution
You are welcome to fork and submit pull requests.
##License
JSONNeverDie is open-sourced software licensed under the MIT license.