快速创建JSON

我想快速创建一个像这样的json:

{

"test1": 0,

"test2": 1435659978,

"test3": 1430479596

}

如何创建此json?

回答:

创建您的对象,在这种情况下为字典:

let dic = ["test1":0, "test2":1435659978, "test3":1430479596]

从对象创建JSON数据:

do {

let dic = ["test1":0, "test2":1435659978, "test3":1430479596]

let jsonData = try NSJSONSerialization.dataWithJSONObject(dic, options: NSJSONWritingOptions.PrettyPrinted)

} catch let error as NSError {

print(error)

}

如果需要,请使用JSON数据作为字符串:

do {

let dic = ["test1":0, "test2":1435659978, "test3":1430479596]

let jsonData = try NSJSONSerialization.dataWithJSONObject(dic, options: NSJSONWritingOptions.PrettyPrinted)

let str = String(data: jsonData, encoding: NSUTF8StringEncoding)

} catch let error as NSError {

print(error)

}

以上是 快速创建JSON 的全部内容, 来源链接: utcz.com/qa/416222.html

回到顶部