简体   繁体   English

如何从 iOS Z818056DBD7E201243206B9C7CD8848 中的 html 获取 JSON

[英]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 ) 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>"

I have tried JSONSerialization.data( but that did not work我已经尝试过JSONSerialization.data(但是没有用

You can Extract JSON string from outputString first您可以先从 outputString 中提取 JSON 字符串

1. Create func in String extension parseString: 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. Implementation: 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"}
}

you can try JSONSerialization.data( again你可以试试JSONSerialization.data(再次

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM