swift中JSONSerialization的ReadingO
2018-06-06 本文已影响317人
山有木枝壮
swift中解析、创建json数据需要用到下面两个参数,下面是这些参数的一些解释
extension JSONSerialization {
@available(iOS 5.0, *)
public struct ReadingOptions : OptionSet {
public init(rawValue: UInt)
public static var mutableContainers: JSONSerialization.ReadingOptions { get }
public static var mutableLeaves: JSONSerialization.ReadingOptions { get }
public static var allowFragments: JSONSerialization.ReadingOptions { get }
}
@available(iOS 5.0, *)
public struct WritingOptions : OptionSet {
public init(rawValue: UInt)
public static var prettyPrinted: JSONSerialization.WritingOptions { get }
/* Sorts dictionary keys for output using [NSLocale systemLocale]. Keys are compared using NSNumericSearch. The specific sorting method used is subject to change.
*/
@available(iOS 11.0, *)
public static var sortedKeys: JSONSerialization.WritingOptions { get }
}
}
一、ReadingOptions
1、mutableContainer
允许将数据解析成json对象后,修改其中的数据
var jsonString:NSString = "{\"names\":[\"James\",\"Jobs\",\"Tom\"]}" as NSString
let jsonData = jsonString.dataUsingEncoding(NSUTF8StringEncoding)
if let jsonObj:NSDictionary = NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions.MutableContainers, error: nil) as? NSDictionary {
//操作之前
print(jsonObj) //James, Jobs, Tom
if let nameArray:NSMutableArray = jsonObj["names"] as? NSMutableArray {
nameArray.addObject("Cook")
}
//操作之后
print(jsonObj) //James, Jobs, Tom, Cook
}
2、mutableLeaves
使解析出来的json对象的叶子节点的属性变为NSMutableString
3、allowFragments
这个参数平常用的比较多,他允许被解析的json数据不是array或者dic包裹,可以是单个string值
var jsonFragmentString = "\"something wrong about api\"" as NSString
let jsonFragmentData = jsonFragmentString.dataUsingEncoding(NSUTF8StringEncoding)
if let jsonObj: AnyObject = NSJSONSerialization.JSONObjectWithData(jsonFragmentData!, options: .AllowFragments, error: nil) {
//使用 AllowFragments 选项,解析成功。
print(jsonObj)
}
二、WritingOptions
1、prettyPrinted
或许的json数据有良好的格式化,方便阅读,对比如下
let jsonObj: [String: Any] = ["name": "xiaoxin", "address": ["province: test", "city: test"]]
do {
let jsonData = try JSONSerialization.data(withJSONObject: jsonObj, options: JSONSerialization.WritingOptions.prettyPrinted)
let jsonString = String.init(data: jsonData, encoding: String.Encoding.utf8)
print(jsonString!)
} catch {
print(error)
}
do {
let jsonData2 = try JSONSerialization.data(withJSONObject: jsonObj, options: [])
let jsonString1 = String.init(data: jsonData2, encoding: String.Encoding.utf8)
print(jsonString1!)
} catch {
print(error)
}
结果:
{
"name" : "xiaoxin",
"address" : [
"province: test",
"city: test"
]
}
{"name":"xiaoxin","address":["province: test","city: test"]}
2、sortedKeys
改选项只有iOS11才提供,按照文档描述,使用该选项会对字典中的keys进行排序
let jsonObj: [String: Any] = ["a": "xiaoxin", "x": ["3": "3test", "2": "2test", "5": "5test"], "d": "dtest" ]
do {
let jsonData = try JSONSerialization.data(withJSONObject: jsonObj, options: JSONSerialization.WritingOptions.prettyPrinted)
let jsonString = String.init(data: jsonData, encoding: String.Encoding.utf8)
print(jsonString!)
} catch {
print(error)
}
do {
let jsonData2 = try JSONSerialization.data(withJSONObject: jsonObj,
options: [JSONSerialization.WritingOptions.prettyPrinted,
JSONSerialization.WritingOptions.sortedKeys])
let jsonString1 = String.init(data: jsonData2, encoding: String.Encoding.utf8)
print(jsonString1!)
} catch {
print(error)
}
结果如下:
{
"d" : "dtest",
"a" : "xiaoxin",
"x" : {
"5" : "5test",
"2" : "2test",
"3" : "3test"
}
}
{
"a" : "xiaoxin",
"d" : "dtest",
"x" : {
"2" : "2test",
"3" : "3test",
"5" : "5test"
}
}
参考:
1、Swift 与 JSON 数据