Json 在 swift 更新
Json updating in swift
目标是将 innerJson 转换为 Array<[String:Any]>。这有什么错误?请帮忙
var outerJson:String? = "{\"outerkey1\":\"Air France\",\"innerJson\":\"[{\\"innerJsonKey1\\":\\"1\\",\\"innerJsonKey2\\":{\\"businessPurpose\\":\\"Test\\",\\"description\\":\\"Testing \\"}},{\\"innerJsonKey1\\":\\"2\\",\\"innerJsonKey2\\":{\\"businessPurpose\\":null,\\"description\\":null}}]\",\"AirClass\":\"null\",\"FromTo\":\"Utti (QVY)\/Umea (UME)\"}"
var convertedDict:[String:Any]?
if let data = outerJson?.data(using: String.Encoding.utf8) {
do {
convertedDict = try JSONSerialization.jsonObject(with: data, options: []) as? [String:Any]
//print(convertedDict)
} catch {
//CALogger.dPrint(error)
}
}
//print(convertedDict?["innerJson"] as Any)
var array = convertedDict?["innerJson"] as? Array<[String:Any]>
print(array)
var outerJson:String? = "{\"outerkey1\":\"Air France\",\"innerJson\":\"[{\\"innerJsonKey1\\":\\"1\\",\\"innerJsonKey2\\":{\\"businessPurpose\\":\\"Test\\",\\"description\\":\\"Testing \\"}},{\\"innerJsonKey1\\":\\"2\\",\\"innerJsonKey2\\":{\\"businessPurpose\\":null,\\"description\\":null}}]\",\"AirClass\":\"null\",\"FromTo\":\"Utti (QVY)\/Umea (UME)\"}"
var convertedDict:[String:Any]?
if let data = outerJson?.data(using: String.Encoding.utf8) {
do {
convertedDict = try JSONSerialization.jsonObject(with: data, options: [.mutableContainers,.allowFragments,.mutableLeaves]) as? [String: Any]
//print(convertedDict)
} catch {
//CALogger.dPrint(error)
}
}
// Create a new array of type [<String, Any>] from the convertedDict dictionary
let arrayOfDicts = convertedDict?.map { [[=10=].key: [=10=].value] }
print(arrayOfDicts)
请仔细阅读JSON。 key innerJson
的值是 a(nother JSON) String
,不是数组。多行文字字符串语法使其更清晰。
你必须申请JSON序列化两次
let outerJson = """
{"outerkey1":"Air France","innerJson":"[{\"innerJsonKey1\":\"1\",\"innerJsonKey2\":{\"businessPurpose\":\"Test\",\"description\":\"Testing \"}},{\"innerJsonKey1\":\"2\",\"innerJsonKey2\":{\"businessPurpose\":null,\"description\":null}}]","AirClass":"null","FromTo":"Utti (QVY)\/Umea (UME)"}
"""
let data = Data(outerJson.utf8)
do {
if let outerDict = try JSONSerialization.jsonObject(with: data) as? [String:Any],
let innerJSON = outerDict["innerJson"] as? String {
if let innerArray = try JSONSerialization.jsonObject(with: Data(innerJSON.utf8)) as? [[String:Any]] {
print(innerArray)
}
}
} catch {
//CALogger.dPrint(error)
}
目标是将 innerJson 转换为 Array<[String:Any]>。这有什么错误?请帮忙
var outerJson:String? = "{\"outerkey1\":\"Air France\",\"innerJson\":\"[{\\"innerJsonKey1\\":\\"1\\",\\"innerJsonKey2\\":{\\"businessPurpose\\":\\"Test\\",\\"description\\":\\"Testing \\"}},{\\"innerJsonKey1\\":\\"2\\",\\"innerJsonKey2\\":{\\"businessPurpose\\":null,\\"description\\":null}}]\",\"AirClass\":\"null\",\"FromTo\":\"Utti (QVY)\/Umea (UME)\"}"
var convertedDict:[String:Any]?
if let data = outerJson?.data(using: String.Encoding.utf8) {
do {
convertedDict = try JSONSerialization.jsonObject(with: data, options: []) as? [String:Any]
//print(convertedDict)
} catch {
//CALogger.dPrint(error)
}
}
//print(convertedDict?["innerJson"] as Any)
var array = convertedDict?["innerJson"] as? Array<[String:Any]>
print(array)
var outerJson:String? = "{\"outerkey1\":\"Air France\",\"innerJson\":\"[{\\"innerJsonKey1\\":\\"1\\",\\"innerJsonKey2\\":{\\"businessPurpose\\":\\"Test\\",\\"description\\":\\"Testing \\"}},{\\"innerJsonKey1\\":\\"2\\",\\"innerJsonKey2\\":{\\"businessPurpose\\":null,\\"description\\":null}}]\",\"AirClass\":\"null\",\"FromTo\":\"Utti (QVY)\/Umea (UME)\"}"
var convertedDict:[String:Any]?
if let data = outerJson?.data(using: String.Encoding.utf8) {
do {
convertedDict = try JSONSerialization.jsonObject(with: data, options: [.mutableContainers,.allowFragments,.mutableLeaves]) as? [String: Any]
//print(convertedDict)
} catch {
//CALogger.dPrint(error)
}
}
// Create a new array of type [<String, Any>] from the convertedDict dictionary
let arrayOfDicts = convertedDict?.map { [[=10=].key: [=10=].value] }
print(arrayOfDicts)
请仔细阅读JSON。 key innerJson
的值是 a(nother JSON) String
,不是数组。多行文字字符串语法使其更清晰。
你必须申请JSON序列化两次
let outerJson = """
{"outerkey1":"Air France","innerJson":"[{\"innerJsonKey1\":\"1\",\"innerJsonKey2\":{\"businessPurpose\":\"Test\",\"description\":\"Testing \"}},{\"innerJsonKey1\":\"2\",\"innerJsonKey2\":{\"businessPurpose\":null,\"description\":null}}]","AirClass":"null","FromTo":"Utti (QVY)\/Umea (UME)"}
"""
let data = Data(outerJson.utf8)
do {
if let outerDict = try JSONSerialization.jsonObject(with: data) as? [String:Any],
let innerJSON = outerDict["innerJson"] as? String {
if let innerArray = try JSONSerialization.jsonObject(with: Data(innerJSON.utf8)) as? [[String:Any]] {
print(innerArray)
}
}
} catch {
//CALogger.dPrint(error)
}