admin管理员组文章数量:1356266
This code fails with an exception indicating invalid JSON:
var example = '{ "AKEY": undefined }';
jQuery.parseJSON(example);
I was able to fix it by replacing all undefineds with empty strings. Are undefineds not part of JSON?
This code fails with an exception indicating invalid JSON:
var example = '{ "AKEY": undefined }';
jQuery.parseJSON(example);
I was able to fix it by replacing all undefineds with empty strings. Are undefineds not part of JSON?
Share Improve this question asked Nov 19, 2010 at 14:32 SapphireSunSapphireSun 9,40811 gold badges49 silver badges61 bronze badges4 Answers
Reset to default 4If you can wrap your head around this, the token undefined
is actually undefined.
Allow me to elaborate: even though JavaScript has a special primitive value called undefined, undefined
is not a JavaScript keyword nor does it have any special meaning. You can break code which tests for the existance of an object by paring to undefined
by defining it.
var obj = { BKEY: 'I exist!' };
if (obj.AKEY == undefined) console.log ('no AKEY');
if (obj.BKEY == undefined) console.log ('should not happen');
undefined='uh oh';
if (obj.AKEY == undefined) console.log ('oops!'); // Logically, we want this to execute, but it will not!
if (obj.BKEY == undefined) console.log ('should not happen');
The only console output will be 'no AKEY'. After we've assigned to the global variable undefined
, obj.AKEY == undefined
bees false because undefined != 'uh oh'
. obj.BKEY == undefined
still returns false, but only because we're lucky. If I had set obj.BKEY='uh oh'
, then obj.BKEY == undefined
would be true, even though it actually exists!
You probably want to explicity set AKEY
to null
. (By the way, null
is a keyword; null='uh oh'
throws an exception).
You could also simply omit AKEY
from your JSON, in which case you would find:
typeof(example.AKEY) == 'undefined'
(If you set AKEY
to null
, then typeof(example.AKEY) == 'object'
.)
The only real difference between setting to null and omitting is whether you want the key to appear in a foreach loop.
No, but null
is. RFC 4627 §2.1:
A JSON value MUST be an object, array, number, or string, or one of the following three literal names:
false null true
var example = '{ "AKEY": null }';
Correct. Undefined and functions are not represented in JSON. http://www.json/js.html
They're not permitted in JSON...look at the alternative and it's clear as to why:
var example = '{}';
var obj = jQuery.parseJSON(example);
obj.AKEY //undefined
If it's undefined
, when you go to access it, it's the same as the key not even being present. Since JSON's primary purpose is for transmitting data (otherwise the broader object literal syntax is fine)...it's better to leave it out altogether.
本文标签: javascriptJSON Parse Error Using jQueryparseJSONStack Overflow
版权声明:本文标题:javascript - JSON Parse Error Using jQuery.parseJSON - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744062605a2584402.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论