我正在使用swiftwkwebview
在webview的repose上我收到了一个json,我如何序列化这个json和; 获取键(状态)的值
let outputString = "<html><head></head><body><pre style=\"word-wrap: break-word; white-space: pre-wrap;\">{\"status\":true,\"data\":{},\"message\":\"authorization success\"}</pre></body></html>"
我尝试了JSONSerialization.data(
,但没有成功
您可以先从outputString中提取JSON字符串
1.在字符串扩展parseString中创建函数:
extension String {
func parseStrring(start: String, to: String) -> String? {
guard let startIndex = self.range(of: start)?.lowerBound, let endIndex = self.range(of: to)?.upperBound else {
return nil
}
return String(self[startIndex..<endIndex])
}
}
2.执行:
let outputString = "<html><head></head><body><pre style=\"word-wrap: break-word; white-space: pre-wrap;\">{\"status\":true,\"data\":{},\"message\":\"authorization success\"}</pre></body></html>"
if let result = outputString.parseStrring(start: "{\"", to: "\"}") { // start from {" to "}
print(result) // will be print {"status":true,"data":{},"message":"authorization success"}
}
您可以再次尝试JSONSerialization.data(