JSON.parse如何管理“未定义"? [英] How does JSON.parse manage 'undefined'?

查看:79
本文介绍了JSON.parse如何管理“未定义"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我改变了这个:

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;
  }
}

对此:

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;
}

如您所见,该代码现在更具可读性和紧凑性.该代码段在正常情况下可以正常运行.问题是,有时我没有在obj对象中包含所有值,因此,我希望这里和那里都有一些undefined.这就是我提出问题的原因:

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. And that is the reason of my questions:

  1. 为什么JSON.parseundefined解释为字符串,例如 MDN undefined的语法错误"?
  2. 然后,我应该在解析值之前检查该值是否是正确的字符串?
  3. 不是JSON.parse,检查何时解析的值是undefined并只返回undefined? (这可能会引起争论,因此,如果您认为这很好,请忽略此问题或声明我的工作方式是错误的)
  4. 如果#2是肯定的,那么只需添加一些条件就可以了,对吧?还是我应该去调用MangaElt的函数并确保obj.listChaps是一个数组,然后在这里忘记JSON.parse? (这始终是一个数组或字符串中的伪数组,由于这是一个协作项目,因此有人可能会有这个原因)
  1. Why JSON.parse interpret a undefined as string, trowing as say the MDN, "syntax error" for 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? (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? 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?. (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

这是已经存在的条目的样子,不会产生错误. 这种情况是促使我提出问题的原因.密钥的类型始终相同,并且需要事先进行测试:

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是字符串
  • mirror是字符串
  • url是字符串
  • listChaps是字符串中的数组"
  • tsupts是整数
  • name is a string
  • mirror is a string
  • url is a string
  • listChaps is an "array" inside a string
  • ts and upts are integers

顺便说一句,obj是一个对象,但是我认为几乎不可能错过.另外,这是一个Chrome扩展程序,但我认为这无关紧要.完整脚本此处.

BTW, obj is an object, but I think that it's almost impossible to miss. Also, this is a Chrome extension, but I don't think that's relevant. Complete script here.

推荐答案

undefined不是有效的JSON令牌.将未定义的值转换为JSON时,正确的做法是将其呈现为null.

undefined is not a valid JSON token. When converting an undefined value to JSON, the correct practice is to render it as null.

这篇关于JSON.parse如何管理“未定义"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆