magento 从RichSnippet JSON中获取数据,并将相同的字符串设置到其他变量中

lsmd5eda  于 2022-11-12  发布在  其他
关注(0)|答案(1)|浏览(79)

我从外部(Reviews-io)脚本生成了这个JSON:https://widget.reviews.co.uk/rich-snippet/dist.js

richSnippet({

        store: "www.storedigital.local",
        sku:"6647;6647_5;6647_4;6647_3;6647_11;6647_10;6647_2;6647_1;6647_9;6647_8;6647_7;6647_6",
        data:{
          "url": "store.stg.gsd.local/1/silla-replica-eames.html",
          "description": ``,
          "mpn": "6647",
          "offers" :[{
            "@type":"Offer",
            "availability": "http://schema.org/InStock",
            "price": "559",
            "priceCurrency": "MXN",
            "url": "https://store.stg.gsd.localx/1/silla-replica-eames.html",
            "priceValidUntil": "2022-05-26",
          }],
          "brand": {
           "@type": "Brand",
           "name": "Not Available",
         }
        }

    })

我需要获取“sku”中的所有数字字符串,然后将它们以相同的格式放在另一个变量中(6647; 6647_1和6647_2)的数据进行比较。
我尝试使用此JS获取数字,但不起作用

var skucollection = JSON.parse(richSnippet, function (key, value) {
   if (key == "sku") {
     return new Sku(value);
    } else {
     return value;
    }
});

你能帮我检查一下我做错了什么吗,以获得这个sku的值字符串,请?

c9qzyr3d

c9qzyr3d1#

解析不是太多吗?就像在内部一样处理它(实际上是JSON)

var richSnippet = {
  store: 'www.storedigital.local',
  sku: '6647;6647_5;6647_4;6647_3;6647_11;6647_10;6647_2;6647_1;6647_9;6647_8;6647_7;6647_6',
  algomas: [],
  data: {
    url: 'store.stg.gsd.local/1/silla-replica-eames.html',
    description: ``,
    mpn: '6647',
    offers: [
      {
        '@type': 'Offer',
        availability: 'http://schema.org/InStock',
        price: '559',
        priceCurrency: 'MXN',
        url: 'https://store.stg.gsd.localx/1/silla-replica-eames.html',
        priceValidUntil: '2022-05-26',
      },
    ],
    brand: {
      '@type': 'Brand',
      name: 'Not Available',
    },
  },
};
var test;
Object.keys(richSnippet).forEach((key) => {
  if (key == 'sku') {
    test = richSnippet[key];
  }
});

console.log('test', test);

相关问题