JSON解析
//MARK: - JSON解析:把NSData转换成object
/** JSON解析:把NSData转换成object */
static func JSONObjectWithData(_ data: Data, options opt: JSONSerialization.ReadingOptions, error: NSErrorPointer? = nil) -> AnyObject?
{
var object:Any?//先声明为任意类型进行计算
do {
object = try JSONSerialization.jsonObject(with: data, options: opt)
} catch let aError as NSError
{
if error != nil
{ error??.pointee = aError }
}
return object as AnyObject?
//后转为专适用于class类型的 AnyObject 类型
}
/** JSON解析:把object转换成NSData */
static func dataWithJSONObject(_ obj: AnyObject, options opt: JSONSerialization.WritingOptions,error: NSErrorPointer? = nil) -> Data? {
var data:Data? do {
data = try JSONSerialization.data(withJSONObject: obj, options: opt)
} catch let aError as NSError
{
if error != nil {
error??.pointee = aError
}
}
return data
}