I want to extract JSON string from html document "without" using third party Framework. I'm trying to create iOS framework and I do not want to use third party Framework in it.
Example url: http://www.nicovideo.jp/watch/sm33786214
In that html, there is a line:
I need to extract: JSON_String_I_want_to extract and convert it to JSON object.
With third party framework "Kanna", it is like this:
if let doc = Kanna.HTML(html: html, encoding: String.Encoding.utf8) {
if let descNode = doc.css("#js-initial-watch-data[data-api-data]").first {
let dataApiData = descNode["data-api-data"]
if let data = dataApiData?.data(using: .utf8) {
if let json = try? JSON(data: data, options: JSONSerialization.ReadingOptions.mutableContainers) {
I searched the web with similar question but unable to apply to my case:(I need to admit I'm not quite following regular expression)
if let html = String(data:data, encoding:.utf8) {
let pattern = "data-api-data=\"(.*?)\".*?>"
let regex = try! NSRegularExpression(pattern: pattern, options: .caseInsensitive)
let matches = regex.matches(in: html, options: [], range: NSMakeRange(0, html.count))
var results: [String] = []
matches.forEach { (match) -> () in
results.append( (html as NSString).substring(with: match.rangeAt(1)) )
}
if let stringJSON = results.first {
let d = stringJSON.data(using: String.Encoding.utf8)
if let json = try? JSONSerialization.jsonObject(with: d!, options: []) as? Any {
// it does not get here...
}
Anyone expert in extracting from html and convert it to JSON?
Thank you.
WKWebViewfor this, which will parse the HTML, and then you'd ask it for the contents of that html tag that holds the json. A little bit heavy solution, though.