【问题标题】:How to format a javascript object to a JSON object for schema data?如何将 javascript 对象格式化为模式数据的 JSON 对象?
【发布时间】:2018-10-26 07:28:12
【问题描述】:

我在下面提到了动态 js 对象形式,我尝试将其转换为模式数据并添加到网页的 head 标记中以用于 SEO 目的。 不知道怎么可能,因为我是这方面的新员工。

我的 JS 函数:

function populateJsonLDScript(data) {
    if (typeof (data) != "undefined" && data  != null) {
        var finalSchemaObj = {};
        var tempSchemaItems = [];
        for (var i = 0; i < data.length; i++) {
            var tempSchemaData = {};
            tempSchemaData["@type"] = "ListItem";
            tempSchemaData["position"] = data[i].DisplayOrder;
            tempSchemaData["item"] = {
                "@id": data[i].CanonicalURL,
                "name": data[i].Name
            };
            tempSchemaItems.push(tempSchemaData);
        }

        for (var i = 0; i < tempSchemaItems.length; ++i) {
            finalSchemaObj[i] = tempSchemaItems[i];
        }

        var scriptSchema = document.createElement('script');
        scriptSchema.type = 'application/ld+json';
        scriptSchema.text = JSON.stringify({
            "@context": "http://schema.org",
            "@type": "BreadcrumbList",
            "itemListElement": [finalSchemaObj]
        });
        if ($('head script[type="application/ld+json"]').length > 0) {
            $('head script[type="application/ld+json"]').remove();
            $("head").append(scriptSchema);
        } else {
            $("head").append(scriptSchema);
        }
    }
}

输出

{
    "@context": "http://schema.org",
    "@type": "BreadcrumbList",
    "itemListElement": [{
        "0": {
            "@type": "ListItem",
            "position": 0,
            "item": {
                "@id": "http://example.com",
                "name": "Home"
            }
        },
        "1": {
            "@type": "ListItem",
            "position": 1,
            "item": {
                "@id": "http://example.com/jewelry",
                "name": "Jewelry"
            }
        },
        "2": {
            "@type": "ListItem",
            "position": 2,
            "item": {
                "@id": "http://example.com/jewelry/necklaces-pendants",
                "name": "Necklaces & Pendants"
            }
        }
    }]
}

但谷歌仍然说它的格式无效。所以,我想将上面的内容格式化为 -

{"@context":"http://schema.org","@type":"BreadcrumbList","itemListElement":[
        {
            "@type":"ListItem",
             "position":0,
             "item": {
                       "@id":"http://example.com",
                       "name":"Home"
                     }
        },
        {
           "@type":"ListItem",
            "position":1,
            "item":{
                     "@id":"http://example.com/jewelry",
                     "name":"Jewelry"
                   }
        },
        {
           "@type":"ListItem",
           "position":2,
           "item":{
                   "@id":"http://example.com/jewelry/necklaces-pendants",
                   "name":"Necklaces & Pendants"
                }
        }
    ]
}

有人请帮忙怎么做吗?

【问题讨论】:

    标签: javascript jquery arrays javascript-objects


    【解决方案1】:

    不要使用对象,使用数组并推入它。像这样的:

    function populateJsonLDScript(data) {
      if (typeof(data) != "undefined" && data != null) {
        var finalSchemaObj = []; // <----- use array here
        var tempSchemaItems = [];
        for (var i = 0; i < data.length; i++) {
          var tempSchemaData = {};
          tempSchemaData["@type"] = "ListItem";
          tempSchemaData["position"] = data[i].DisplayOrder;
          tempSchemaData["item"] = {
            "@id": data[i].CanonicalURL,
            "name": data[i].Name
          };
          tempSchemaItems.push(tempSchemaData);
        }
    
        for (var i = 0; i < tempSchemaItems.length; ++i) {
          finalSchemaObj.push(tempSchemaItems[i]); // <--- push here
        }
    
        var scriptSchema = document.createElement('script');
        scriptSchema.type = 'application/ld+json';
        scriptSchema.text = JSON.stringify({
          "@context": "http://schema.org",
          "@type": "BreadcrumbList",
          "itemListElement": finalSchemaObj // <----- and use finalSchemaObj here
        });
        if ($('head script[type="application/ld+json"]').length > 0) {
          $('head script[type="application/ld+json"]').remove();
          $("head").append(scriptSchema);
        } else {
          $("head").append(scriptSchema);
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-20
      • 2018-07-18
      • 2020-10-19
      • 1970-01-01
      • 1970-01-01
      • 2019-03-20
      • 2020-11-05
      相关资源
      最近更新 更多