文本 var 到 json 数组

Text var to json array

我正在尝试配置 header 出价前出价。我正在使用 php + mysql 从数据库中加载我想要的广告,并使用来自 php.

的数据在 javascript 中创建一个文本变量

当我必须创建变量 "adUnits" 时,问题就来了。如果我手动创建它直接写它工作正常但创建相同的内容 javascript 我不明白它的工作。

这是有效的代码

var adUnits = [{
     code: '/123/468x60',
     mediaTypes: {
         banner: {
             sizes: [
                 [468, 60]
             ]
         }
     },
     bids: [{
         bidder: 'adserver',
         params: {
             networkId: 1234
         }
     }]
 }, {
     code: '/123/336x280',
     mediaTypes: {
         banner: {
             sizes: [
                 [336, 280],
                 [300, 250]
             ]
         }
     },
     bids: [{
         bidder: 'adserver',
         params: {
             networkId: 1234
         }
     }]
 }, {
     code: '/123/300x600',
     mediaTypes: {
         banner: {
             sizes: [
                 [300, 600],
                 [300, 250]
             ]
         }
     },
     bids: [{
         bidder: 'adserver',
         params: {
             networkId: 1234
         }
     }]
 }];

这是行不通的代码

var arrayadsname = adsname.split('#');//content of arrayadsname => /123/468x60#/123/336x280#/123/300x600
var arrayadssize = adssize.split('#');//content of arrayadssize => 468x60#336,280|300,250#300,600|300,250
var arraysize = '';
var ad = '';
var adUnits = [];
var preadUnits = '';

for (var i = 0; i < arrayadsname.length; i++) {  
        var arraysizeparts = arrayadssize[i].split('|');

    for (var j = 0; j < arraysizeparts.length; j++) {
                if (j == 0){
                        arraysize = '[' + arraysizeparts[j] + ']';
                }else{
                        arraysize = arraysize + ',' + '[' + arraysizeparts[j] + ']';
                }
            }

        ad = '{code: \'' + arrayadsname[i] + '\', mediaTypes: {banner: {sizes: ' + '[' + arraysize + ']' + '}}, bids: [{bidder: \'adserver\', params: {networkId: 1234}}]}';

        if (i == 0) preadUnits = ad;
        else preadUnits = preadUnits + ',' + ad;
}
//Result of ad => {code: '/123/468x60', mediaTypes: {banner: {sizes: [[468,60]]}}, bids: [{bidder: 'adserver', params: {networkId: 1234}}]},{code: '/123/336x280', mediaTypes: {banner: {sizes: [[336,280],[300,250]]}}, bids: [{bidder: 'adserver', params: {networkId: 1234}}]},{code: '/123/300x600', mediaTypes: {banner: {sizes: [[300,600],[300,250]]}}, bids: [{bidder: 'adserver', params: {networkId: 1234}}]}


adUnits = "[" + JSON.stringify(preadUnits) + "]";

我已经尝试了所有可能的组合,将变量声明为单元,如 object、数组、文本,并使用 JSON.stringify、JSON.parse 也声明了 "arraysize",如数组并解析它.不知道是哪个问题。

我在控制台中看到这个错误:"callee: [Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them at Arguments.invokeGetter"

提前致谢

首先为adssize中的每一项构造一个sizes的数组——例如,将'300,600|300,250'变成[[300, 600], [300, 250]]。然后,您可以通过映射代码(由 # 拆分)并访问刚刚构造的 sizes 数组上的适当索引来创建 adUnits 对象:

const codes = '/123/468x60#/123/336x280#/123/300x600'.split('#');
const sizes = '468x60#336,280|300,250#300,600|300,250'
  .split('#')
  .map(
    // str is something like: '300,600|300,250'
    str => str.split('|').map(
      // substr is something like: '300,600'
      substr => substr.split(/[,x]/).map(Number)
    )
  );

const adUnits = codes.map((code, i) => ({
  code,
  mediaTypes: {
    banner: {
      sizes: sizes[i]
    }
  },
  bids: [{
     bidder: 'adserver',
     params: {
         networkId: 1234
     }
  }]
}));
console.log(adUnits);

如果它必须是 JSON 而不是一个对象,那么之后 JSON.stringify 它(永远不要尝试手动构造 JSONs):

const codes = '/123/468x60#/123/336x280#/123/300x600'.split('#');
const sizes = '468x60#336,280|300,250#300,600|300,250'
  .split('#')
  .map(
    // str is something like: '300,600|300,250'
    str => str.split('|').map(
      // substr is something like: '300,600'
      substr => substr.split(/[,x]/).map(Number)
    )
  );

const adUnits = codes.map((code, i) => ({
  code,
  mediaTypes: {
    banner: {
      sizes: sizes[i]
    }
  },
  bids: [{
     bidder: 'adserver',
     params: {
         networkId: 1234
     }
  }]
}));
console.log(JSON.stringify(adUnits, null, 2));