我使用的是Swift WKWebView
在webview的repose上,我收到一个json,我怎么能把json序列化& 得到一个key的值(状况)
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. 在String扩展中创建func 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. 实现方式:1.在String扩展中创建func。
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(
再次