[英]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: 这就是我提问的原因:
JSON.parse
interpret a undefined
as string, trowing as say the MDN , "syntax error" for undefined
? 为什么JSON.parse
将undefined
字符串解释为字符串,如同MDN一样翻译 , undefined
“语法错误”? 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) (这可能会引发争论,所以,如果你认为这样做很好,那就忽略这个问题,或者说我对我的低谷问题是错的) 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 ts
和upts
是整数 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 . 这里完成脚本。
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
的可接受参数 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.