你能帮我吗, 如果JSON带有类似的多行是
,我将面临问题"{"groupId":"58","chat":"send 2linesnsecondline"}"
我正在接受服务器的响应,并使用此功能转换
let dataDic = self.convertToDictionary(text: (remoteMessage.appData["message"]! as AnyObject) as! String)
print(dataDic!)
这是我的功能
func convertToDictionary(text: String) -> [String: AnyObject]? {
if let data = text.data(using: String.Encoding.utf8) {
do {
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String:AnyObject]
return json
} catch {
print(error.localizedDescription)
}
}
return nil
}
但是,如果代码具有多条线,则出现问题,因为它在返回中放置 n,并且 它给了我
The data couldn’t be read because it isn’t in the correct format
Error Domain=NSCocoaErrorDomain Code=3840 "Unescaped control character around character 145." UserInfo={NSDebugDescription=Unescaped control character around character 145.}
在解析JSON之前,您应该在" n"之前放一个额外的" "。尝试使用"替换"功能。这样,您的JSON在解析之前就需要格式化。