Javascript NodeJS-如何刮除ld+;json数据并将其保存到对象
我一直在试图找到一种方法来获取aplication/ld+json内容并将其保存到本地对象。我想要的是将它保存到一个对象中,在我的程序中,我将能够控制台.log(data.offers.availability),这将导致日志记录:“InStock”,这将用于每个数据值 我目前有:Javascript NodeJS-如何刮除ld+;json数据并将其保存到对象,javascript,node.js,json,ld,Javascript,Node.js,Json,Ld,我一直在试图找到一种方法来获取aplication/ld+json内容并将其保存到本地对象。我想要的是将它保存到一个对象中,在我的程序中,我将能够控制台.log(data.offers.availability),这将导致日志记录:“InStock”,这将用于每个数据值 我目前有: let content = JSON.stringify($("script[type='application/ld+json']").html())
let content = JSON.stringify($("script[type='application/ld+json']").html())
let filteredJson = content.replace(/\\n/g, '')
let results = JSON.parse(filteredJson)
console.log(results)
这导致:-不允许我使用console.log(results.offers.availability)
我正在尝试刮取和保存的数据:
正如Bergi所指出的,问题在于您在已经是字符串的内容上使用了
JSON.stringify
,但出于好奇,我自己也尝试过。考虑下面的测试:
index.html(通过localhost:4000提供):
result
现在是一个对象,它保存来自脚本标记和日志的数据result.offers.availability
,将按预期打印InStock
。正如Bergi指出的,问题是您正在对已经是字符串的内容使用JSON.stringify
,但出于好奇,我自己也试过了。考虑下面的测试:
index.html(通过localhost:4000提供):
result
现在是一个对象,它保存来自脚本标记和日志的数据result.offers.availability
,将按预期打印InStock
。呃,不要使用JSON.stringify
?另外,我建议不要获取转义实体。@Bergi:有趣的是,我得到了语法错误:JSON输入的意外结束当我对示例代码使用.text()
时,它与.html()
一起工作,或者与我这样做的方式一起工作.text()
似乎返回一个空字符串。@eol您在节点中使用的是什么DOM实现,什么是$
?我记得有一个问题,一些浏览器没有脚本的文本内容。但无论如何,.html()
和.text()
都返回字符串,JSON.stringify()
都是错误的。要删除换行符,请替换/\n/g
而不是/\\n/g
。我正在使用cheerio。经过一些调试后,我发现以下行:。根据这一点,如果当前元素的标记名是script
,则它们不会下降/重复出现,因此将返回一个空字符串。呃,不要使用JSON.stringify
?另外,我建议不要获取转义实体。@Bergi:有趣的是,我得到了语法错误:JSON输入的意外结束当我对示例代码使用.text()
时,它与.html()
一起工作,或者与我这样做的方式一起工作.text()
似乎返回一个空字符串。@eol您在节点中使用的是什么DOM实现,什么是$
?我记得有一个问题,一些浏览器没有脚本的文本内容。但无论如何,.html()
和.text()
都返回字符串,JSON.stringify()
都是错误的。要删除换行符,请替换/\n/g
而不是/\\n/g
。我正在使用cheerio。经过一些调试后,我发现以下行:。根据这一点,如果当前元素的标记名是script
,则它们不会下降/重复出现,因此将返回一个空字符串。嘿!非常感谢你花时间帮助我。这在其他同样使用application/ld+json的站点上也有效。然而,在这个网站上(这个问题的基础上),它得到了一个错误(未定义:5),纯粹是因为描述内容太长,其中有空格。通过添加“jsonRaw2=jsonRaw.replace(/\n/g,”),然后解析jsonRaw2.Hey,可以很容易地解决这个问题!非常感谢你花时间帮助我。这在其他同样使用application/ld+json的站点上也有效。然而,在这个网站上(这个问题的基础上),它得到了一个错误(未定义:5),纯粹是因为描述内容太长,其中有空格。通过添加“jsonRaw2=jsonRaw.replace(/\n/g,”),然后解析jsonRaw2,可以很容易地解决这个问题。
{ "@context": "http://schema.org/",
"@type": "Product", "name": "Apex Legends - Bangalore - Mini Epics",
"description": "<div class="textblock"><p><h2>Apex Legends - Bangalore - Mini Epics </h2><p>Helden uit alle uithoeken van de wereld strijden voor eer, roem en fortuin in Apex Legends. Weta Workshop betreedt the Wild Frontier en brengt Bangalore met zich mee - Mini Epics style!</p><p>Verzamel alle Apex Legends Mini Epics en voeg ook Bloodhound en Mirage toe aan je collectie!</p></p></div>",
"brand": {
"@type": "Thing",
"name": "Game Mania"
},
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": "5",
"ratingCount": "2"
},
"offers": {
"@type": "Offer",
"priceCurrency": "EUR",
"price": "19.98",
"availability" : "InStock"
}
}
<html>
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "Product",
"name": "Apex Legends - Bangalore - Mini Epics",
"offers": {
"@type": "Offer",
"priceCurrency": "EUR",
"price": "19.98",
"availability": "InStock"
}
}
</script>
<body>
<h2>Index</h2>
</body>
</html>
const superagent = require('superagent');
const cheerio = require('cheerio');
(async () => {
const response = await superagent("http://localhost:4000");
const $ = cheerio.load(response.text);
// note that I'm not using .html(), although it works for me either way
const jsonRaw = $("script[type='application/ld+json']")[0].children[0].data;
// do not use JSON.stringify on the jsonRaw content, as it's already a string
const result = JSON.parse(jsonRaw);
console.log(result.offers.availability);
})()