解析错误JSON.parse(意外令牌u)

时间:2016-03-11 06:53:00

标签: javascript arrays json

var storage = chrome.storage.local;
var cachedStorage = {};

这是js文件。虽然我已正确解析,但它显示了意外的令牌。它还显示了其html源页面的意外令牌。任何人都建议我如何对此进行排序。

var defaultStorage = [{
  savedPatterns: JSON.stringify([
    [{
      "en": "English"
    }, {
      "it": "Italian"
    }, "25", true],
    [{
      "en": "English"
    }, {
      "la": "Latin"
    }, "15", false]
  ]),

}];
  

错误发生在这里意外的令牌u

function createPattern() {
  console.log('createPattern begin');
  var patterns = JSON.parse(S('savedPatterns'));
  var srce = [],
    trg = [],
    prb = [];
  console.log(S('savedPatterns'));
  console.debug(S('savedPatterns'));
  var translator = document.getElementById('translatorService');
  var service = translator.children[translator.selectedIndex].value;
  srce[0] = document.getElementById('sourceLanguage');
  srce[1] = srce[0].children[srce[0].selectedIndex].value;
  srce[2] = srce[0].children[srce[0].selectedIndex].text;
  trg[0] = document.getElementById('targetLanguage');
  trg[1] = trg[0].children[trg[0].selectedIndex].value;
  trg[2] = trg[0].children[trg[0].selectedIndex].text;
  prb[0] = document.getElementById('translationProbability');
  prb[1] = prb[0].children[prb[0].selectedIndex].value;

  patterns.push([
    [srce[1], srce[2]],
    [trg[1], trg[2]],
    prb[1],
    false,
    service
  ]);
  saveBulk({
    'savedPatterns': JSON.stringify(patterns)
  }, 'Saved Pattern');
  console.log('createPattern end');
}

function S(key) {
  return cachedStorage[key];
}

function loadStorageAndUpdate(callback) {
  storage.get(null, function(data) {
    console.log('data: ' + data + ' : ' + JSON.stringify(data));
    var d = {};
    if (!data || JSON.stringify(data) == '{}') { // in this case, storage was not initialized yet
      console.log('setting storage to defaultStorage (stringified): ');
      console.log(JSON.stringify(defaultStorage));
      storage.set(defaultStorage);
      d = defaultStorage;
    } else {
      d = data;
    }

    cachedStorage = d;
    if (!!callback) {
      callback(d);
    }
  });
}

2 个答案:

答案 0 :(得分:1)

Unexpected token失败并且取决于字符(在这种情况下为JSON.parse)时出现错误u,您可以假设其原因。

  • u is value is undefined
  • o如果value是object

try {
  JSON.parse(undefined)
} catch (ex) {
  document.write(ex.message + "<br/>")
}

try {
  JSON.parse({})
} catch (ex) {
  document.write(ex.message)
}

您可以尝试这样的事情:

function s(key) {
  var obj = {
    foo: "foo",
    bar: "bar"
  }
  var v = null;

  try {
    v = JSON.parse(obj[key]);
  } catch () {
    v = obj[key];
  }

  return v;
}

function main() {
  var v = s("foo");
}

有关更多信息,请参阅以下帖子。 Uncaught SyntaxError: Unexpected token with JSON.parse

答案 1 :(得分:0)

您正在尝试解析undefined。当您致电JSON.parse()时,您正在传递S('savedPatterns'),而cachedStorage['savedPatterns']又会尝试访问undefined,但会以cachedStorage开头,您无法解析。您可以将var cachedStorage = { savedPatterns: JSON.stringify([]) }; 初始化为:

/*** in your unit test ***/
//... get your grid object ...
const gridComponent = ....

// save the original function to assign it later back
const originalOn = gridComponent.on;

// now mock the on function
gridComponent.on = function(actionName, handler){
assert.ok(true, "on() function has been called");

assert.equal(actionName, "rowclick", "the action on which on() function has been triggered is correct");
}

// execute tested context
this.$('#row0grid_testgrid').trigger('click');

// tidy up
gridComponent.on = originalOn;