[英]How to get JSON from html in iOS swift
I am using swift WKWebView
on repose of webview I am receiving one json, how can I serialise that JSON & get a value of key ( status )
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扩展parseString中创建func:
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(
再次
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.