在Javascript中解析Json-Google标记管理器

在Javascript中解析Json-Google标记管理器,javascript,json,google-tag-manager,Javascript,Json,Google Tag Manager,我有这样的剧本: function() { var publishedDate = ""; var yoastInfo = JSON.parse(document.head.querySelector('.yoast-schema-graph.yoast-schema-graph--main').innerHTML); vars = yoastInfo["@graph"]; for(var i in vars) { if(vars[i]['@type'] === 'Web

我有这样的剧本:

function() {
  var publishedDate = "";
  var yoastInfo = JSON.parse(document.head.querySelector('.yoast-schema-graph.yoast-schema-graph--main').innerHTML);
  vars = yoastInfo["@graph"];
  for(var i in vars) {
    if(vars[i]['@type'] === 'WebPage') {
      pubDateRaw=(vars[i]['datePublished']);
      break;
    }
  }
  publishedDate.concat(pubDateRaw.substring(0,4),pubDateRaw.substring(5,7),pubDateRaw.substring(8,10));
  return publishedDate;
}
当我运行代码时,它会给我一个
,而不是日期

以下是json:

{
  "@context": "https://schema.org",
  "@graph": [   

    {
      "@type": "WebPage",
      "@id": "id234234",
      "url": "https://www.whatevver.com",
      "inLanguage": "de-DE",
      "name": "this is the name",
      "isPartOf": {
        "@id": "id234234
      },
      "primaryImageOfPage": {
        "@id": "id234234"
      },
      "datePublished": "2020-01-14T07:46:12+00:00",
      "dateModified": "2020-01-15T08:04:50+00:00",
      "description": "description "
    },

  ]
}
所以我需要从json中提取datePubished。我的代码有什么问题

编辑

在你们的帮助下,我制作了以下脚本:

function() {
  var publishedDate = "";
  var yoastInfo = JSON.parse(document.head.querySelector('.yoast-schema-graph.yoast-schema-graph--main').innerHTML); 
  vars = yoastInfo["@graph"];
  for(var i in vars) {
    if(vars[i]['@type'] === 'WebPage') {
      publishedDate=(vars[i]['datePublished']); 
    }
  }



  return publishedDate;
}
现在我需要将日期时间
2020-01-14T07:46:12+00:00
缩短到
2020-01-14
最好的解决方案是什么

EDIT2

在以下位置找到解决方案:

给定JSON:

{
  "@context": "https://schema.org",
  "@graph": [   

    {
      "@type": "WebPage",
      "@id": "id234234",
      "url": "https://www.whatevver.com",
      "inLanguage": "de-DE",
      "name": "this is the name",
      "isPartOf": {
        "@id": "id234234" // MAKE IT AS A STRING
      },
      "primaryImageOfPage": {
        "@id": "id234234"
      },
      "datePublished": "2020-01-14T07:46:12+00:00",
      "dateModified": "2020-01-15T08:04:50+00:00",
      "description": "description "
    },

  ]
}
请按照代码中的注释进行澄清。谢谢:))

并查看链接以了解更多说明:

基本上,您只想从给定的JSON返回datePublised?您没有在for循环中尝试一些调试内容吗?似乎这个条件永远不会满足是的,基本上我想返回datePubished-并将其推送到数据层。好的,我知道了。您希望日期格式如何?如果更新了我的问题,结果应该是YYYY-MM-DDYeah,对Tagmanager(ES6)稍加修改即可。也许你能再帮我一次?现在我得到了完整的时间日期字符串,但我只需要YYYY-MM-dd函数formatDate(Date){var d=new-Date(Date),month=''+(d.getMonth()+1),day=''+d.getDate(),year=d.getFullYear();if(month.length<2)month='0'+month;if(day.length<2)day='0'+day;返回[year,month,day].join(“/”);}formatDate(publishedDate);您可以使用moment.js进行更多简化。
function() {
  let publishedDate = "";
  const yoastInfo = JSON.parse(document.head.querySelector('.yoast-schema- 
  graph.yoast-schema-graph--main').innerHTML); // I HOPE YOURE ABLE TO EXTRACT THE JSON HERE IN yoastInfo.
  vars = yoastInfo["@graph"];
  for(var i in vars) {
    if(vars[i]['@type'] === 'WebPage') {
      publishedDate=(vars[i]['datePublished']); // YOU ARE GETTING EMPTY STRING BECAUSE YOU'RE SETTING VARIABLE CALLED 'publishedDate' AND SETTING VALUE IN 'pubDateRaw'.
    }
  }
  return publishedDate;
}