html - 仅使用 iOS API 从 html 中提取 JSON 字符串
问题描述
我想使用第三方框架“不”从 html 文档中提取 JSON 字符串。我正在尝试创建 iOS 框架,我不想在其中使用第三方框架。
示例网址: http ://www.nicovideo.jp/watch/sm33786214
在那个html中,有一行:
我需要提取: JSON_String_I_want_to 提取并将其转换为 JSON 对象。
使用第三方框架“Kanna”,它是这样的:
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) {
我在网上搜索了类似的问题,但无法适用于我的案例:(我需要承认我并没有完全遵循正则表达式)
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...
}
任何从 html 中提取并将其转换为 JSON 的专家?
谢谢你。
解决方案
您pattern
似乎并不坏,只是 HTML 元素的属性值可能正在使用字符实体。
在将字符串解析为 JSON 之前,您需要将它们替换为实际字符。
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, range: NSRange(0..<html.utf16.count)) //<-USE html.utf16.count, NOT html.count
var results: [String] = []
matches.forEach {match in
let propValue = html[Range(match.range(at: 1), in: html)!]
//### You need to replace character entities into actual characters
.replacingOccurrences(of: """, with: "\"")
.replacingOccurrences(of: "'", with: "'")
.replacingOccurrences(of: ">", with: ">")
.replacingOccurrences(of: "<", with: "<")
.replacingOccurrences(of: "&", with: "&")
results.append(propValue)
}
if let stringJSON = results.first {
let dataJSON = stringJSON.data(using: .utf8)!
do {
let json = try JSONSerialization.jsonObject(with: dataJSON)
print(json)
} catch {
print(error) //You should not ignore errors silently...
}
} else {
print("NO result")
}
}
推荐阅读
- c++ - 为 C++ 开发配置 Opensuse Leap 15.2
- laravel - Laravel 码头工人部署
- html - 当有圆角时,如何阻止主题背景颜色泄漏?
- python - 'urllib' 无限循环
- python - 带有验证令牌的 Python 发布请求
- python - 按数量分组获得前 3 名
- scala - Spark数据框替换scala中不同数据类型的空值
- python - 无法从 Docker 容器访问本地主机
- three.js - Three.js无法读取未定义的属性“制服”
- sql - /register 关系“small_small_hr_staffprofile”处的 ProgrammingError 不存在