【发布时间】:2020-02-06 22:20:24
【问题描述】:
我正在遍历 JSON-LD 对象以创建用户评论列表,这在 JSFiddle (https://jsfiddle.net/vmgn1ykb/) 中有效,但是当我使用任一示例将其添加到我的页面并在 Google 结构化数据测试工具上进行测试时我收到错误“JSON-LD 语法错误:需要值、对象或数组”。并且代码没有运行。请帮助我理解原因。
我已经尝试过改变
var arrayLength = jsonld['review'].length;
类似
JSON.parse(document.querySelector('script[type="application/ld+json"]').innerText)
和
JSON.parse(document.querySelector('script[id="jsonData"]').innerText)
但代码仍然无法运行,并且 GSDTT 中的错误仍然存在。 请帮助理解我做错了什么。
这行得通,
var jsonld = {
"@context": "http://schema.org",
"@type": "Product",
"image": "https://www.myurl.com/media/db3e3b23f81585_M.jpg",
"name": "Test name",
"description": "Test review desc.",
"offers": {
"@type": "AggregateOffer",
"availability": "http://schema.org/InStock",
"highPrice": "5195.00",
"lowPrice": "2595.00",
"offerCount": "1",
"priceCurrency": "ZAR",
"priceValidUntil": "2020-09-30",
"url": "https://www.myurl.com/"
}
,
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": "5",
"bestRating": "5",
"reviewCount": "2"
},
"review": [
{
"@type": "Review",
"author": " Meagen",
"description": "Test review desc",
"reviewRating": {
"@type": "Rating",
"bestRating": "5",
"ratingValue": "5",
"worstRating": "1"
}},
{
"@type": "Review",
"author": " Ericka",
"description": "Test review desc",
"reviewRating": {
"@type": "Rating",
"bestRating": "5",
"ratingValue": "5",
"worstRating": "1"
}}
]}
var arrayLength = jsonld['review'].length;
outline = document.createElement('div');
outline.className = 'outline';
document.getElementsByTagName('body')[0].appendChild(outline);
for (i = 0; i < arrayLength; i++) {
inside = document.createElement('div');
inside.className = 'inside';
document.getElementsByClassName('outline')[0].appendChild(inside);
//desc
desc = document.createElement('div');
desc.className = 'desc';
desc.innerHTML = JSON.stringify(jsonld.review[i].description);
document.getElementsByClassName('inside')[i].appendChild(desc);
//auth
auth = document.createElement('p');
auth.className = 'author';
auth.innerHTML = jsonld.review[i].author + " rated this tour " + jsonld.review[i].reviewRating.ratingValue + " out of 5";
document.getElementsByClassName('inside')[i].appendChild(auth);
//score
score = document.createElement('p');
score.className = 'score';
score.innerHTML = jsonld.review[i].reviewRating.ratingValue + " out of 5";
document.getElementsByClassName('inside')[i].appendChild(score);
}
但是当我使用脚本标签 type="application/ld+json 将它添加到我的页面时,这不会,
<script id="jsonData" type="application/ld+json">
var jsonld = {
"@context": "http://schema.org",
"@type": "Product",
"image": "https://www.myurl.com/media/db3e3b23f81585_M.jpg",
"name": "Test name",
"description": "Test review desc.",
"offers": {
"@type": "AggregateOffer",
"availability": "http://schema.org/InStock",
"highPrice": "5195.00",
"lowPrice": "2595.00",
"offerCount": "1",
"priceCurrency": "ZAR",
"priceValidUntil": "2020-09-30",
"url": "https://www.myurl.com/"
}
,
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": "5",
"bestRating": "5",
"reviewCount": "2"
},
"review": [
{
"@type": "Review",
"author": " Meagen",
"description": "Test review desc",
"reviewRating": {
"@type": "Rating",
"bestRating": "5",
"ratingValue": "5",
"worstRating": "1"
}},
{
"@type": "Review",
"author": " Ericka",
"description": "Test review desc",
"reviewRating": {
"@type": "Rating",
"bestRating": "5",
"ratingValue": "5",
"worstRating": "1"
}}
]}
</script>
<script type="text/javascript">
var arrayLength = jsonld['review'].length;
outline = document.createElement('div');
outline.className = 'outline';
document.getElementsByTagName('body')[0].appendChild(outline);
for (i = 0; i < arrayLength; i++) {
inside = document.createElement('div');
inside.className = 'inside';
document.getElementsByClassName('outline')[0].appendChild(inside);
//desc
desc = document.createElement('div');
desc.className = 'desc';
desc.innerHTML = JSON.stringify(jsonld.review[i].description);
document.getElementsByClassName('inside')[i].appendChild(desc);
//auth
auth = document.createElement('p');
auth.className = 'author';
auth.innerHTML = jsonld.review[i].author + " rated this tour " + jsonld.review[i].reviewRating.ratingValue + " out of 5";
document.getElementsByClassName('inside')[i].appendChild(auth);
//score
score = document.createElement('p');
score.className = 'score';
score.innerHTML = jsonld.review[i].reviewRating.ratingValue + " out of 5";
document.getElementsByClassName('inside')[i].appendChild(score);
}
</script>
【问题讨论】:
-
SDTT 可以运行的 JavaScript 非常有限。另外,我认为 JavaScript 在类型 application/ld+json 中无效。执行 json = 有效地将其转换为 JavaScript 命令以将字符串添加到变量中。它不是一个 json 对象。
-
感谢@TonyMcCreath 在我的以下更新中我删除了 var 声明,因为我确实认为 JS 不会在 JSON-LD 中验证,我最终做的是,
var jsonld = JSON.parse(document.querySelector("#jsonData").innerText);在我的脚本中抓取选择器和返回和对象。我假设 JSON 保持不变。
标签: javascript json-ld google-schemas structured-data