JSON Parse Error Using jQuery.parseJSON

后端 未结 4 1757
情深已故
情深已故 2020-12-12 04:29

This code fails with an exception indicating invalid JSON:

var example = \'{ \"AKEY\": undefined }\';
jQuery.parseJSON(example);

I was able

4条回答
  •  醉酒成梦
    2020-12-12 04:50

    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.

提交回复
热议问题