简体   繁体   English

JSON.parse如何管理'undefined'?

[英]How does JSON.parse manage 'undefined'?

I transformed this: 我改变了这个:

function MangaElt(obj) {
  "use strict";
  this.mirror = obj.mirror;
  this.name = obj.name;
  this.url = obj.url;
  if (obj.lastChapterReadURL !== undefined) {
    this.lastChapterReadURL = obj.lastChapterReadURL;
    this.lastChapterReadName = obj.lastChapterReadName;
  } else {
    this.lastChapterReadURL = null;
    this.lastChapterReadName = null;
  }
  this.listChaps = [];
  if (obj.listChaps !== undefined && obj.listChaps !== null && obj.listChaps !== "null") {
    if (!isArray(obj.listChaps)) {
      this.listChaps = JSON.parse(obj.listChaps);
    }
  }
  this.read = 0;
  if (obj.read !== undefined && obj.read !== null && obj.read !== "null") {
    this.read = obj.read;
  }
}

Into this: 进入:

function MangaElt(obj) {
  "use strict";
  this.mirror = obj.mirror;
  this.name = obj.name;
  this.url = obj.url;
  this.lastChapterReadURL = obj.lastChapterReadURL || null;
  this.lastChapterReadName = obj.lastChapterReadName || null;
  this.listChaps = JSON.parse(obj.listChaps) || [];
  this.read = obj.read || 0;
  this.update = obj.update || 1;
}

As you can see, the code is now more readable and compact. 如您所见,代码现在更具可读性和紧凑性。 The snippet works under normal circumstances just fine. 该片段在正常情况下工作正常。 The thing is that I don't have sometimes all the values in the obj object, so, I expect some undefined 's here and there. 问题是我有时没有obj对象中的所有值,所以,我希望这里和那里有一些undefined的值。 And that is the reason of my questions: 这就是我提问的原因:

  1. Why JSON.parse interpret a undefined as string, trowing as say the MDN , "syntax error" for undefined ? 为什么JSON.parseundefined字符串解释为字符串,如同MDN一样翻译undefined “语法错误”?
  2. Should then, I, check before the values get parsed if the value is a proper string? 那么,如果值是正确的字符串,我应该在解析值之前检查吗?
  3. Shouldn't JSON.parse, check whenever the value parsed is undefined and just return undefined ? 不应该是JSON.parse,检查解析的值是否undefined并且只返回undefined (This may rise arguments, so, if you believe that is good as is, just ignore this question or state that I'm just wrong with my train of trough) (这可能会引发争论,所以,如果你认为这样做很好,那就忽略这个问题,或者说我对我的低谷问题是错的)
  4. If #2 is affirmative, then just adding some conditional as the first snipped should be enough, right? 如果#2是肯定的,那么只要添加一些条件作为第一个剪切应该就够了,对吧? Or should I go to the function that calls MangaElt and make sure that obj.listChaps is an array and forget about JSON.parse here?. 或者我应该去调用MangaElt的函数并确保obj.listChaps是一个数组并忘记JSON.parse吗? ( This is always an array or a pseudo-array in a string, and since this is a collaborative project, someone may have a reason for this) 这总是数组或字符串中的伪数组,因为这是一个协作项目,有人可能有理由这样做)

For the curious that may ask, 'what's the error you are getting?' 对于可能会问的好奇心,“你得到的错误是什么?” is this: 这是:

Error in event handler for 'undefined': Unexpected token u SyntaxError: Unexpected token u
at Object.parse (native)
at new MangaElt (chrome-extension://nhjloagockgobfpopemejpgjjechcpfd/js/MangaElt.js:44:25)
at readManga (chrome-extension://nhjloagockgobfpopemejpgjjechcpfd/js/background.js:410:24)
at chrome-extension://nhjloagockgobfpopemejpgjjechcpfd/js/background.js:607:9
at Event.dispatchToListener (event_bindings:356:21)
at Event.dispatch_ (event_bindings:342:27)
at Event.dispatch (event_bindings:362:17)
at miscellaneous_bindings:165:24
at Event.dispatchToListener (event_bindings:356:21)
at Event.dispatch_ (event_bindings:342:27) event_bindings:346

EDIT: This is what already existing entries looks like , which do not generate errors. 编辑: 这是现有条目的样子 ,不会产生错误。 This scenario is what motivated my question. 这个场景是我的问题的动机。 The type of keys are always the same and are tested beforehand: 键的类型始终相同,并事先进行测试:

  • name is a string name是一个字符串
  • mirror is a string mirror是一个字符串
  • url is a string url是一个字符串
  • listChaps is an "array" inside a string listChaps是一个字符串中的“数组”
  • ts and upts are integers tsupts是整数

BTW, obj is an object, but I think that it's almost impossible to miss. 顺便说一句, obj是一个对象,但我认为几乎不可能错过。 Also, this is a Chrome extension, but I don't think that's relevant. 此外,这是一个Chrome扩展程序,但我不认为这是相关的。 Complete script here . 这里完成脚本。

  1. undefined is not a legal token in a JSON file (see www.json.org) nor is it an acceptable parameter to JSON.parse undefined不是JSON文件中的合法标记(请参阅www.json.org),也不是JSON.parse的可接受参数
  2. Your code is not semantically identical to the previous version. 您的代码在语义上与先前版本不同。 Many of the tests in the previous version are more exhaustive (and less error prone) than your new version 以前版本中的许多测试比新版本更详尽(并且更不容易出错)
  3. Why "refactor" that code if worked as desired? 为什么“重构”该代码如果按预期工作? You have not refactored it - you've broken it. 你还没有重构它 - 你已经破坏了它。

undefined is not a valid JSON token. undefined不是有效的JSON令牌。 When converting an undefined value to JSON, the correct practice is to render it as null. 将未定义的值转换为JSON时,正确的做法是将其呈现为null。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM