如何将字典数组转换为 JSON?
How to convert array of dictionary to JSON?
我有一组字典,我想将其转换为 JSON。我的对象是 [[String: AnyObject]]
类型,并希望以这样的示例结束:
[
{ "abc": 123, "def": "ggg", "xyz": true },
{ "abc": 456, "def": "hhh", "xyz": false },
{ "abc": 789, "def": "jjj", "xyz": true }
]
这就是我正在尝试的,但编译器不喜欢我的声明:
extension Array where Element == Dictionary<String, AnyObject> {
var json: String {
do { return try? NSJSONSerialization.dataWithJSONObject(self, options: []) ?? "[]" }
catch { return "[]" }
}
}
我该怎么做?
一个简单的实现方法是扩展 CollectionType。
使用可选的绑定和向下转换,然后序列化为数据,然后转换为字符串。
extension CollectionType where Generator.Element == [String:AnyObject] {
func toJSONString(options: NSJSONWritingOptions = .PrettyPrinted) -> String {
if let arr = self as? [[String:AnyObject]],
let dat = try? NSJSONSerialization.dataWithJSONObject(arr, options: options),
let str = String(data: dat, encoding: NSUTF8StringEncoding) {
return str
}
return "[]"
}
}
let arrayOfDictionaries: [[String:AnyObject]] = [
["abc":123, "def": "ggg", "xyz": true],
["abc":456, "def": "hhh", "xyz": false]
]
print(arrayOfDictionaries.toJSONString())
输出:
[
{
"abc" : 123,
"def" : "ggg",
"xyz" : true
},
{
"abc" : 456,
"def" : "hhh",
"xyz" : false
}
]
我认为您在尝试扩展 Array
时运气不佳,因为扩展实体时不可能满足相同类型的要求。
你可以做的就是利用免费功能:
func jsonify(array: [[String:AnyObject]]) {
...
}
这样你就可以保持你想要的类型安全,代价是移动函数位置。
Swift 4:
extension Collection where Iterator.Element == [String:AnyObject] {
func toJSONString(options: JSONSerialization.WritingOptions = .prettyPrinted) -> String {
if let arr = self as? [[String:AnyObject]],
let dat = try? JSONSerialization.data(withJSONObject: arr, options: options),
let str = String(data: dat, encoding: String.Encoding.utf8) {
return str
}
return "[]"
}
}
用法:
let arrayOfDictionaries = [
{ "abc": 123, "def": "ggg", "xyz": true },
{ "abc": 456, "def": "hhh", "xyz": false },
{ "abc": 789, "def": "jjj", "xyz": true }
]
print(arrayOfDictionaries.toJSONString())
Swift 5,我不得不将其更改为 Any
而不是 AnyObject
,构建 Eric & [=22 的解决方案=].
extension Collection where Iterator.Element == [String: Any] {
func toJSONString(options: JSONSerialization.WritingOptions = .prettyPrinted) -> String {
if let arr = self as? [[String: Any]],
let dat = try? JSONSerialization.data(withJSONObject: arr, options: options),
let str = String(data: dat, encoding: String.Encoding.utf8) {
return str
}
return "[]"
}
}
用法:
let arrayOfDictionaries = [
{ "abc": 123, "def": "ggg", "xyz": true },
{ "abc": 456, "def": "hhh", "xyz": false },
{ "abc": 789, "def": "jjj", "xyz": true }
]
print(arrayOfDictionaries.toJSONString())
我有一组字典,我想将其转换为 JSON。我的对象是 [[String: AnyObject]]
类型,并希望以这样的示例结束:
[
{ "abc": 123, "def": "ggg", "xyz": true },
{ "abc": 456, "def": "hhh", "xyz": false },
{ "abc": 789, "def": "jjj", "xyz": true }
]
这就是我正在尝试的,但编译器不喜欢我的声明:
extension Array where Element == Dictionary<String, AnyObject> {
var json: String {
do { return try? NSJSONSerialization.dataWithJSONObject(self, options: []) ?? "[]" }
catch { return "[]" }
}
}
我该怎么做?
一个简单的实现方法是扩展 CollectionType。
使用可选的绑定和向下转换,然后序列化为数据,然后转换为字符串。
extension CollectionType where Generator.Element == [String:AnyObject] {
func toJSONString(options: NSJSONWritingOptions = .PrettyPrinted) -> String {
if let arr = self as? [[String:AnyObject]],
let dat = try? NSJSONSerialization.dataWithJSONObject(arr, options: options),
let str = String(data: dat, encoding: NSUTF8StringEncoding) {
return str
}
return "[]"
}
}
let arrayOfDictionaries: [[String:AnyObject]] = [
["abc":123, "def": "ggg", "xyz": true],
["abc":456, "def": "hhh", "xyz": false]
]
print(arrayOfDictionaries.toJSONString())
输出:
[
{
"abc" : 123,
"def" : "ggg",
"xyz" : true
},
{
"abc" : 456,
"def" : "hhh",
"xyz" : false
}
]
我认为您在尝试扩展 Array
时运气不佳,因为扩展实体时不可能满足相同类型的要求。
你可以做的就是利用免费功能:
func jsonify(array: [[String:AnyObject]]) {
...
}
这样你就可以保持你想要的类型安全,代价是移动函数位置。
Swift 4:
extension Collection where Iterator.Element == [String:AnyObject] {
func toJSONString(options: JSONSerialization.WritingOptions = .prettyPrinted) -> String {
if let arr = self as? [[String:AnyObject]],
let dat = try? JSONSerialization.data(withJSONObject: arr, options: options),
let str = String(data: dat, encoding: String.Encoding.utf8) {
return str
}
return "[]"
}
}
用法:
let arrayOfDictionaries = [
{ "abc": 123, "def": "ggg", "xyz": true },
{ "abc": 456, "def": "hhh", "xyz": false },
{ "abc": 789, "def": "jjj", "xyz": true }
]
print(arrayOfDictionaries.toJSONString())
Swift 5,我不得不将其更改为 Any
而不是 AnyObject
,构建 Eric & [=22 的解决方案=].
extension Collection where Iterator.Element == [String: Any] {
func toJSONString(options: JSONSerialization.WritingOptions = .prettyPrinted) -> String {
if let arr = self as? [[String: Any]],
let dat = try? JSONSerialization.data(withJSONObject: arr, options: options),
let str = String(data: dat, encoding: String.Encoding.utf8) {
return str
}
return "[]"
}
}
用法:
let arrayOfDictionaries = [
{ "abc": 123, "def": "ggg", "xyz": true },
{ "abc": 456, "def": "hhh", "xyz": false },
{ "abc": 789, "def": "jjj", "xyz": true }
]
print(arrayOfDictionaries.toJSONString())