Jasoom - Swift 包装器
jopen
9年前
Jasoom 是 Swift 包装器,它使得
使用更为方便,安全。JSON 解析:
do { let json = try JSON.parseData(data) if json["name"].isUndefined { throw .MissingName } if json["info"]["age"].isUndefined { throw .MissingAge } let model = MyModel( name: json["name"].stringValue! age: json["info"]["age"].intValue! married: json["status"]["married"].boolValue ?? false firstCar: json["cars"][0].stringValue ?? "" ) } catch { // Handle error }
JSON 生成:
var object = JSON.object() object["name"] = .String("Bob Smith") object["info"] = JSON.object() object["info"]["age"] = .Number(10) object["status"] = JSON.object() object["status"]["married"] = .Number(true) object["cars"] = JSON.array() object["cars"].append(.String("Ford Mustang")) do { let data = try object.generateData() } catch { // Handle error }