如何使用javascript拆分这个json数据?我希望在不同的变量中存储content.rendered
json作为第一个p
标签,图像作为第二个p
标签,文件作为第三个p
标签,以便在html页面中显示它。你能在这个拆分上帮我吗?你能像在sippet中提到的那样,从json数组中分离出每个content.rendered吗?
[{
"author": 1,
"content": {
"rendered": "<p>Welcome to WordPress. This is your first post. Edit or delete it, then start writing!</p>\n<p><a href=\"file.pdf\"></a></p>\n<p><img src=\"image.jpg\"/></p>",
"protected": false
}
},
{
"author": 1,
"content": {
"rendered": "<p>Welcome to WordPress. This is your first post. Edit or delete it, then start writing!</p>\n<p><a href=\"file.pdf\"></a></p>\n<p><img src=\"image.jpg\"/></p>",
"protected": false
}
},
{
"author": 1,
"content": {
"rendered": "<p>Welcome to WordPress. This is your first post. Edit or delete it, then start writing!</p>\n<p><a href=\"file.pdf\"></a></p>\n<p><img src=\"image.jpg\"/></p>",
"protected": false
}
}]
发布于 2018-08-20 04:27:58
首先,必须使用JSON.parse
解析JSON,然后需要解析存储在content.rendered
中的超文本标记语言。
下面这样的代码应该可以工作。
const str = `{
"author": 1,
"content": {
"rendered": "<p>Welcome to WordPress. This is your first post. Edit or delete it, then start writing!</p>\\n<p><a href=\\"file.pdf\\"></a></p>\\n<p><img src=\\"image.jpg\\"/></p>",
"protected": false
}
}`
let json = JSON.parse(str)
let content = document.createElement('div')
content.innerHTML = json.content.rendered
let description = content.querySelector('p').innerText
let image = content.querySelector('img').src
let file = content.querySelector('a').href
let result = {
description,
image,
file
}
https://stackoverflow.com/questions/51921448
复制相似问题