【发布时间】:2019-05-27 05:11:29
【问题描述】:
我想将从网络请求收到的 HTML 转换为 JSON,以便我可以轻松读取值。
如果有办法直接从 HTML 读取值,请在评论中告诉我
我找到了这个库:https://github.com/andrejewski/himalaya
但是当我尝试在我的应用程序中运行该示例时,我收到错误 Cannot read property prototype of undefined 所以我认为这个库在 react-native 中不起作用,因为它包含一个核心 Node JS 模块。
这是代码:
import {parse} from 'himalaya'
const html = this.state.html
const json = parse(html)
console.log('????', json)
一些示例 html 可能是:
<div class='post post-featured'>
<p>Himalaya parsed me...</p>
<!-- ...and I liked it. -->
</div>
这个 html 的预期输出是:
[{
type: 'element',
tagName: 'div',
attributes: [{
key: 'class',
value: 'post post-featured'
}],
children: [{
type: 'element',
tagName: 'p',
attributes: [],
children: [{
type: 'text',
content: 'Himalaya parsed me...'
}]
}, {
type: 'comment',
content: ' ...and I liked it. '
}]
}]
是否有另一种方法/库可以在 react native 中将 HTML 转换为 JSON?
【问题讨论】:
-
如果您提供您编写的代码,我可能会提供帮助。看来您需要在客户端使用
window.himalaya.parse(html)。 -
我添加了代码(我直接从文档中获取)
-
你有权限修改服务器的响应吗?
-
您是否有一些要转换的 html?
-
我无权修改服务器的响应,这是一个 3rd 方 api。
标签: javascript html json react-native