美文网首页
JSON.swift

JSON.swift

作者: 幸运的小强本人 | 来源:发表于2016-03-06 21:01 被阅读64次
    typealias JSON = AnyObject
    typealias JSONDictionary = Dictionary<String, AnyObject>
    typealias JSONArray = Array<AnyObject>
    
    func _JSONString(object: JSON)->String? {
          return object as? String
    }
    
    func _JSONInt(object: JSON)->Int? {
        return object as? Int
    }
    
    func _JSONObject(object: JSON)->JSONDictionary {
        return object as? JSONDictionary
    }
    
    func decodeJSON(data: NSData)->Result<JSON> {
        let jsonOptional: JSON! = try?NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(rawValue:0))
    
        return resultFromOptional(jsonOptional, error: NSError())
    }
    
    func decodeObject<U: JSONDecodable>(json: JSON)->Result<U> {
        return resultFromOptional(U.decode(json), error:NSError())
    }
    

    相关文章

      网友评论

          本文标题:JSON.swift

          本文链接:https://www.haomeiwen.com/subject/sluakttx.html