Swift-JSON 
@dankogai
What is it?
https://github.com/dankogai/swift-json
swift-json is… 
• Wraps lengthy objective-c NSJSONSerialization 
… 
• into concise, swifty JSON 
• even swiftier than SwiftyJSON 
• Works both in Swift 1.0 (Xcode 6.0) and Swift 1.1
Object->JSON 
let obj:[String:AnyObject] = [ 
"array": [JSON.null, false, 0, "", [], [:]], 
"object":[ 
"null": JSON.null, 
"bool": true, 
"int": 42, 
"double": 3.141592653589793, 
"string": "a αt弾nꚲ", 
"array": [], 
"object": [:] 
], 
"url":"http://blog.livedoor.com/dankogai/" 
]
Object->JSON 
let json = JSON(obj)
String->JSON 
let json = JSON( 
string:"{"url":"http://blog.livedoor.com/dankogai/","array": 
[null,false,0,"",[],{}],"object":{"object":{},"bool":true,"null 
":null,"array":[],"int":42,"double":3.141592653589793,"string": 
"a αt弾nꚲ"}}" 
)
URL->JSON 
let json = JSON( 
URL:"http://api.dan.co.jp/asin/4534045220.json" 
)
Tree Traversal 
json["object"]["null"].asNull // NSNull 
json["object"]["bool"].asBool // true 
json["object"]["int"].asInt // 42 
json["object"]["double"].asDouble // 3.141592653589793 
json["object"]["string"].asString // "a αt弾nꚲ" 
json["array"][0].asNull // NSNull() 
json["array"][1].asBool // false 
json["array"][2].asInt // 0 
json["array"][3].asString // ""
Error Handling 
if let b = json["noexistent"][1234567890]["entry"].asBool { 
// .... 
} else { 
let e = json["noexistent"][1234567890]["entry"].asError 
println(e) 
} // Error Domain=JSONErrorDomain Code=404 "["noexistent"] not found" 
UserInfo=0x10064bfc0 {NSLocalizedDescription=["noexistent"] not found}
Type Checking 
json["object"]["null"].type // "NSNull" 
json["object"]["null"].isNull // true 
json["object"]["bool"].type // "Bool" 
json["object"]["bool"].isBool // true 
json["object"]["bool"].isNumber // false 
json["object"]["int"].type // "Int" 
json["object"]["int"].isInt // 42 
json["object"]["int"].isNumber // true 
json["object"]["double"].type // "Double" 
json["object"]["double"].isDouble // true 
json["object"]["double"].isNumber // true 
json["object"]["string"].type // "String" 
json["object"]["string"].isString // true
Schema as Class 
class MyJSON : JSON { 
override init(_ obj:AnyObject){ super.init(obj) } 
override init(_ json:JSON) { super.init(json) } 
var null :NSNull? { return self["null"].asNull } 
var bool :Bool? { return self["bool"].asBool } 
var int :Int? { return self["int"].asInt } 
var double:Double? { return self["double"].asDouble } 
var string:String? { return self["string"].asString } 
var url: String? { return self["url"].asString } 
var array :MyJSON { return MyJSON(self["array"]) } 
var object:MyJSON { return MyJSON(self["object"]) } 
}
Schema as Class 
let myjson = MyJSON(obj) 
myjson.object.null // NSNull? 
myjson.object.bool // Bool? 
myjson.object.int // Int? 
myjson.object.double // Double? 
myjson.object.string // String? 
myjson.url // String?
One More Thing 
• Software Design で次号から連 
載します
you.thank! 
for q in questions { 
q.answer 
}

Ios8yahoo swift-json

  • 1.
  • 2.
  • 3.
  • 4.
    swift-json is… •Wraps lengthy objective-c NSJSONSerialization … • into concise, swifty JSON • even swiftier than SwiftyJSON • Works both in Swift 1.0 (Xcode 6.0) and Swift 1.1
  • 5.
    Object->JSON let obj:[String:AnyObject]= [ "array": [JSON.null, false, 0, "", [], [:]], "object":[ "null": JSON.null, "bool": true, "int": 42, "double": 3.141592653589793, "string": "a αt弾nꚲ", "array": [], "object": [:] ], "url":"http://blog.livedoor.com/dankogai/" ]
  • 6.
  • 7.
    String->JSON let json= JSON( string:"{"url":"http://blog.livedoor.com/dankogai/","array": [null,false,0,"",[],{}],"object":{"object":{},"bool":true,"null ":null,"array":[],"int":42,"double":3.141592653589793,"string": "a αt弾nꚲ"}}" )
  • 8.
    URL->JSON let json= JSON( URL:"http://api.dan.co.jp/asin/4534045220.json" )
  • 9.
    Tree Traversal json["object"]["null"].asNull// NSNull json["object"]["bool"].asBool // true json["object"]["int"].asInt // 42 json["object"]["double"].asDouble // 3.141592653589793 json["object"]["string"].asString // "a αt弾nꚲ" json["array"][0].asNull // NSNull() json["array"][1].asBool // false json["array"][2].asInt // 0 json["array"][3].asString // ""
  • 10.
    Error Handling iflet b = json["noexistent"][1234567890]["entry"].asBool { // .... } else { let e = json["noexistent"][1234567890]["entry"].asError println(e) } // Error Domain=JSONErrorDomain Code=404 "["noexistent"] not found" UserInfo=0x10064bfc0 {NSLocalizedDescription=["noexistent"] not found}
  • 11.
    Type Checking json["object"]["null"].type// "NSNull" json["object"]["null"].isNull // true json["object"]["bool"].type // "Bool" json["object"]["bool"].isBool // true json["object"]["bool"].isNumber // false json["object"]["int"].type // "Int" json["object"]["int"].isInt // 42 json["object"]["int"].isNumber // true json["object"]["double"].type // "Double" json["object"]["double"].isDouble // true json["object"]["double"].isNumber // true json["object"]["string"].type // "String" json["object"]["string"].isString // true
  • 12.
    Schema as Class class MyJSON : JSON { override init(_ obj:AnyObject){ super.init(obj) } override init(_ json:JSON) { super.init(json) } var null :NSNull? { return self["null"].asNull } var bool :Bool? { return self["bool"].asBool } var int :Int? { return self["int"].asInt } var double:Double? { return self["double"].asDouble } var string:String? { return self["string"].asString } var url: String? { return self["url"].asString } var array :MyJSON { return MyJSON(self["array"]) } var object:MyJSON { return MyJSON(self["object"]) } }
  • 13.
    Schema as Class let myjson = MyJSON(obj) myjson.object.null // NSNull? myjson.object.bool // Bool? myjson.object.int // Int? myjson.object.double // Double? myjson.object.string // String? myjson.url // String?
  • 14.
    One More Thing • Software Design で次号から連 載します
  • 15.
    you.thank! for qin questions { q.answer }