简体   繁体   English

application / ld + json和javascript数据交换

[英]application/ld+json and javascript data exchange

I have some jQuery code which constructs an array called myList[] . 我有一些jQuery代码构造一个名为myList[]的数组。 This array appears in the console like this: ["2 items", "3items", "so on"] , so this part goes well. 这个数组出现在控制台中,如下所示: ["2 items", "3items", "so on"] ,所以这部分进展顺利。

<script type="text/javascript">
var myList = [];
function buld myList(){
...
}

I need to pass myList[] to the application/ld+json like 我需要将myList[]传递给application/ld+json类的

<script type="application/ld+json">
{
    "@context": "http://schema.org/",
    "@type": "Recipe",
    "recipeIngredients": myList, //this one doesn't work 
}

.. ..

How can I pass the values from javascript to the application/ld+json ? 如何将值从javascript传递给application/ld+json Thanks in advance! 提前致谢!

Please try this: 请试试这个:

<script id="myJSONID" type="application/ld+json"></script>

Then: 然后:

var myList = [];

function buildMyList() {
    return ["2 items", "3items", "so on"];
}

$("#myJSONID").text(function() {
    return JSON.stringify({
        "@context": "http://schema.org/",
        "@type": "Recipe",
        "recipeIngredient": buildMyList()
    });
});

Or: 要么:

<script type="text/javascript">
  var myList = [];    
  function buildMyList() {
      return ["2 items", "3items", "so on"];
  }

  var el = document.createElement('script');
  el.type = 'application/ld+json';

  el.text = JSON.stringify({
        "@context": "http://schema.org/",
        "@type": "Recipe",
        "recipeIngredient": buildMyList()
    });

  document.querySelector('body').appendChild(el);
</script>

Demo: https://jsfiddle.net/iRbouh/9po7dtg4/ 演示: https //jsfiddle.net/iRbouh/9po7dtg4/

Note: Please make sure you change recipeIngredients to recipeIngredient , singular. 注意:请确保将recipeIngredients更改为recipeIngredient ,singular。 (Thank you @AlexKudryashev). (谢谢@AlexKudryashev)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 AEM JSP 页面上传递值<script type=“text/javascript”> to <script type=“application/ld+json”> - On AEM JSP page pass value of <script type=“text/javascript”> to <script type=“application/ld+json”> 从 application/ld+Json 脚本中获取“@type”的值 - Grabbing the value of “@type” from application/ld+Json script NodeJS - 如何抓取 ld+json 数据并将其保存到 object - NodeJS - how to scrape ld+json data and save it to an object ld + json格式化错误格式 - ld+json formatting error formatting 如何从中获取内容json <scriptscript type=“application/ld+json”> - how can get content json from <scriptscript type=“application/ld+json”> 如何处理请求 header 在 react.js 中接受 application/ld+json 获取请求 - how to handle request header accept application/ld+json in react.js get request 如何在 html label 中使用 javascript 修改 type(ld+json) 值? - How can I modify the type(ld+json) value with javascript in html label? 在 Angular2 组件模板中添加 ld+json 脚本标签 - Adding ld+json script tags in Angular2 component template 在客户端 React 中添加 ld+json 脚本标签 - Add ld+json script tag in client-side React 对于 Astro.js,如何访问 ld+json schema.org SEO JSON 中的 frontmatter 变量? - For Astro.js, how to access frontmatter variables in ld+json schema.org SEO JSON?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM