在 swift 5 中将字符串转换为字典?
convert string to dictionary in swift 5?
我有一个包含 json 的字符串,格式如下:
var myString: String = "{key1: val1, key2: val2}"
如何将其转换为:
var myJson: [String: String] = ["key1": "val1", "key2": "val2"]
由于我的字符串不是 "{"key1": "val1", "key2": "val2"}"
格式,我在通过以下代码片段解析它时遇到了困难:
func convertToDictionary(text: String) -> [String: Any]? {
if let data = text.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
} catch {
print(error.localizedDescription)
}
}
return nil
}
这个函数对于 myString
总是 returns nil
你可能不得不以艰难的方式去做。像这样:
import SwiftUI
@main
struct TestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
extension String {
func trim() -> String {
return self.trimmingCharacters(in: .whitespacesAndNewlines)
}
}
struct ContentView: View {
var body: some View {
Text("testing")
.onAppear {
let myString: String = "{key1: val1, key2: val2, key3: val3}"
let myJson = transform(myString)
print("----> myJson: \(myJson)")
}
}
func transform(_ myString: String) -> [String : String] {
var result = [String : String]()
if myString.count > 2 { // case of {}
let temp1 = myString.dropFirst().dropLast()
let temp2 = temp1.split(separator: ",").map{String([=10=])}
for str in temp2 {
let temp3 = str.split(separator: ":").map{String([=10=])}
let (k,v) = (temp3[0].trim(),temp3[1].trim())
result[k] = v
}
}
return result
}
}
您知道应该引用键和值。这是一个简单的方法。
let jsonString = myString.replacingOccurrences(of: "(\w+)", with: "\"\"", options: .regularExpression)
let result = convertToDictionary(text: jsonString)
我有一个包含 json 的字符串,格式如下:
var myString: String = "{key1: val1, key2: val2}"
如何将其转换为:
var myJson: [String: String] = ["key1": "val1", "key2": "val2"]
由于我的字符串不是 "{"key1": "val1", "key2": "val2"}"
格式,我在通过以下代码片段解析它时遇到了困难:
func convertToDictionary(text: String) -> [String: Any]? {
if let data = text.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
} catch {
print(error.localizedDescription)
}
}
return nil
}
这个函数对于 myString
总是 returns nil你可能不得不以艰难的方式去做。像这样:
import SwiftUI
@main
struct TestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
extension String {
func trim() -> String {
return self.trimmingCharacters(in: .whitespacesAndNewlines)
}
}
struct ContentView: View {
var body: some View {
Text("testing")
.onAppear {
let myString: String = "{key1: val1, key2: val2, key3: val3}"
let myJson = transform(myString)
print("----> myJson: \(myJson)")
}
}
func transform(_ myString: String) -> [String : String] {
var result = [String : String]()
if myString.count > 2 { // case of {}
let temp1 = myString.dropFirst().dropLast()
let temp2 = temp1.split(separator: ",").map{String([=10=])}
for str in temp2 {
let temp3 = str.split(separator: ":").map{String([=10=])}
let (k,v) = (temp3[0].trim(),temp3[1].trim())
result[k] = v
}
}
return result
}
}
您知道应该引用键和值。这是一个简单的方法。
let jsonString = myString.replacingOccurrences(of: "(\w+)", with: "\"\"", options: .regularExpression)
let result = convertToDictionary(text: jsonString)