替换" \" iOS Swift 4中String的值

时间:2018-03-08 12:47:19

标签: ios swift string swift4

我有如下所示的字符串,我想替换" \"这与""但Xcode不允许我这样做。它给出错误,如#34;未终止的字符串文字"

let jsonStr = "[{\"isSelected\":true,\"languageProficiencies\":[{\"isSelected\":true,\"name\":\"Advance\"},{\"isSelected\":false,\"name\":\"Proficient\"},{\"isSelected\":false,\"name\":\"Basic\"},{\"isSelected\":false,\"name\":\"Below Basic\"}],\"name\":\"English\"},{\"isSelected\":false,\"languageProficiencies\":[{\"isSelected\":false,\"name\":\"Advance\"},{\"isSelected\":false,\"name\":\"Proficient\"},{\"isSelected\":false,\"name\":\"Basic\"},{\"isSelected\":false,\"name\":\"Below Basic\"}],\"name\":\"Malay\"},{\"isSelected\":false,\"languageProficiencies\":[{\"isSelected\":false,\"name\":\"Advance\"},{\"isSelected\":false,\"name\":\"Proficient\"},{\"isSelected\":false,\"name\":\"Basic\"},{\"isSelected\":false,\"name\":\"Below Basic\"}],\"name\":\"Chinese\"},{\"isSelected\":false,\"languageProficiencies\":[{\"isSelected\":false,\"name\":\"Advance\"},{\"isSelected\":false,\"name\":\"Proficient\"},{\"isSelected\":false,\"name\":\"Basic\"},{\"isSelected\":false,\"name\":\"Below Basic\"}],\"name\":\"Tamil\"}]"

替换代码" \"到""是:

let newString = jsonStr.replacingOccurrences(of: "\", with: "", options: .literal, range: nil)

Error Image

4 个答案:

答案 0 :(得分:1)

您需要将其转换为JSON。

let string = "[{\"isSelected\":true,\"languageProficiencies\":[{\"isSelected\":true,\"name\":\"Advance\"},{\"isSelected\":false,\"name\":\"Proficient\"},{\"isSelected\":false,\"name\":\"Basic\"},{\"isSelected\":false,\"name\":\"Below Basic\"}],\"name\":\"English\"},{\"isSelected\":false,\"languageProficiencies\":[{\"isSelected\":false,\"name\":\"Advance\"},{\"isSelected\":false,\"name\":\"Proficient\"},{\"isSelected\":false,\"name\":\"Basic\"},{\"isSelected\":false,\"name\":\"Below Basic\"}],\"name\":\"Malay\"},{\"isSelected\":false,\"languageProficiencies\":[{\"isSelected\":false,\"name\":\"Advance\"},{\"isSelected\":false,\"name\":\"Proficient\"},{\"isSelected\":false,\"name\":\"Basic\"},{\"isSelected\":false,\"name\":\"Below Basic\"}],\"name\":\"Chinese\"},{\"isSelected\":false,\"languageProficiencies\":[{\"isSelected\":false,\"name\":\"Advance\"},{\"isSelected\":false,\"name\":\"Proficient\"},{\"isSelected\":false,\"name\":\"Basic\"},{\"isSelected\":false,\"name\":\"Below Basic\"}],\"name\":\"Tamil\"}]"
let data = string.data(using: .utf8)!
do {
    if let jsonArray = try JSONSerialization.jsonObject(with: data, options : .allowFragments) as? [Dictionary<String, Any>]
    {
          print(jsonArray)
    } else {
        print("json can not be formed !!")
    }
} catch {
   print(error)
}

如果您想要替换“/”,那么......

print(jsonStr.replacingOccurrences(of: "\\", with: ""))

答案 1 :(得分:1)

我尝试过这个问题时遇到同样的问题

extension Data
{
func dataToJSON() -> Any? {
    do {
        return try JSONSerialization.jsonObject(with: self, options: [])
    } catch let myJSONError {
        print(myJSONError)
    }
    return nil
}
}


extension String
{        
    func decodeUrl() -> String
    {
        return self.removingPercentEncoding!
    }
}

用法:

let yourJsonString = "".decodeUrl()
        if let data = yourJsonString.data(using: .utf8) {
            if let json = data.dataToJSON() {
                print(json)
            }
        }

答案 2 :(得分:1)

请使用&#34; \\&#34;例如下面的例子。

let jsonStr = "[{\"isSelected\":true,\"languageProficiencies\":[{\"isSelected\":true,\"name\":\"Advance\"},{\"isSelected\":false,\"name\":\"Proficient\"},{\"isSelected\":false,\"name\":\"Basic\"},{\"isSelected\":false,\"name\":\"Below Basic\"}],\"name\":\"English\"},{\"isSelected\":false,\"languageProficiencies\":[{\"isSelected\":false,\"name\":\"Advance\"},{\"isSelected\":false,\"name\":\"Proficient\"},{\"isSelected\":false,\"name\":\"Basic\"},{\"isSelected\":false,\"name\":\"Below Basic\"}],\"name\":\"Malay\"},{\"isSelected\":false,\"languageProficiencies\":[{\"isSelected\":false,\"name\":\"Advance\"},{\"isSelected\":false,\"name\":\"Proficient\"},{\"isSelected\":false,\"name\":\"Basic\"},{\"isSelected\":false,\"name\":\"Below Basic\"}],\"name\":\"Chinese\"},{\"isSelected\":false,\"languageProficiencies\":[{\"isSelected\":false,\"name\":\"Advance\"},{\"isSelected\":false,\"name\":\"Proficient\"},{\"isSelected\":false,\"name\":\"Basic\"},{\"isSelected\":false,\"name\":\"Below Basic\"}],\"name\":\"Tamil\"}]"
let convertedStr = jsonStr.replacingOccurrences(of: "\\", with: "", options: .literal, range: nil)
print(convertedStr)

答案 3 :(得分:1)

let jsonStr = "[{\"isSelected\":true,\"languageProficiencies\":[{\"isSelected\":true,\"name\":\"Advance\"},{\"isSelected\":false,\"name\":\"Proficient\"},{\"isSelected\":false,\"name\":\"Basic\"},{\"isSelected\":false,\"name\":\"Below Basic\"}],\"name\":\"English\"},{\"isSelected\":false,\"languageProficiencies\":[{\"isSelected\":false,\"name\":\"Advance\"},{\"isSelected\":false,\"name\":\"Proficient\"},{\"isSelected\":false,\"name\":\"Basic\"},{\"isSelected\":false,\"name\":\"Below Basic\"}],\"name\":\"Malay\"},{\"isSelected\":false,\"languageProficiencies\":[{\"isSelected\":false,\"name\":\"Advance\"},{\"isSelected\":false,\"name\":\"Proficient\"},{\"isSelected\":false,\"name\":\"Basic\"},{\"isSelected\":false,\"name\":\"Below Basic\"}],\"name\":\"Chinese\"},{\"isSelected\":false,\"languageProficiencies\":[{\"isSelected\":false,\"name\":\"Advance\"},{\"isSelected\":false,\"name\":\"Proficient\"},{\"isSelected\":false,\"name\":\"Basic\"},{\"isSelected\":false,\"name\":\"Below Basic\"}],\"name\":\"Tamil\"}]"

这些\就是说:不要解释下一个",他们逃避它。如果你没有,因为它们用于分隔字符串的开头/结尾,它是一个转义序列:\"

其中最常用的一个是\n,如果你不使用\,它会读n(字母),而不是新的线。 嗯,对于双引号,它是相同的:不要解释为&#34;代码&#34;用于分隔String的开始/结束的值。

您可能需要阅读this

如果你print("jsonStr:\n \(jsonStr)"),你将拥有控制台:

$>jsonStr:
[{"isSelected":true,"languageProficiencies":[{"isSelected":true,"name":"Advance"},{"isSelected":false,"name":"Proficient"},{"isSelected":false,"name":"Basic"},{"isSelected":false,"name":"Below Basic"}],"name":"English"},{"isSelected":false,"languageProficiencies":[{"isSelected":false,"name":"Advance"},{"isSelected":false,"name":"Proficient"},{"isSelected":false,"name":"Basic"},{"isSelected":false,"name":"Below Basic"}],"name":"Malay"},{"isSelected":false,"languageProficiencies":[{"isSelected":false,"name":"Advance"},{"isSelected":false,"name":"Proficient"},{"isSelected":false,"name":"Basic"},{"isSelected":false,"name":"Below Basic"}],"name":"Chinese"},{"isSelected":false,"languageProficiencies":[{"isSelected":false,"name":"Advance"},{"isSelected":false,"name":"Proficient"},{"isSelected":false,"name":"Basic"},{"isSelected":false,"name":"Below Basic"}],"name":"Tamil"}]

没有\见过。

对于其他人来说,包装不好,但逻辑更多。

let jsonData = jsonStr.data(using: .utf8)
let jsonArray = try! JSONSerialization.jsonObject(with: jsonData!, options: [])
print("jsonArray:\n \(jsonArray)")