数组或者字典解析为json字符串
2016-06-07 本文已影响69人
JackMayx
///数组或者字典解析为json字符串
func toJSONString(dict:NSDictionary!)->NSString{
let data = try? NSJSONSerialization.dataWithJSONObject(dict, options: NSJSONWritingOptions.PrettyPrinted)
let strJson = NSString(data: data!, encoding: NSUTF8StringEncoding)
return strJson!
}
代码示例:
let dic = ["method":"SignUp.getAuthKey","version":"V1.0.0","loginNo":["18708133592","13880****62","76****7"]]
let jsonDic = toJSONString(dic);
print("jsonString=\(jsonDic)");
///结果
jsonString={
"method" : "SignUp.getAuthKey",
"loginNo" : [
"18708133592",
"13880****62",
"76****7"
],
"version" : "V1.0.0"
}