为什么JSON.stringify-Function将string.Empty("")转换为"null"-String?问题,我为什么不使用:
JSON.parse(json, function(key, value) {
if (typeof value === 'string') {
if (value == 'null')
return '';
return value;
}
});
Run Code Online (Sandbox Code Playgroud)
...是,如果有人真的写"空"(非常不可能,但可能),我有一个问题......
谢谢你的每一个答案!
小智 1
现在解决这个问题的最好方法是,将“document.getElementById('id').value”表达式打包到 String 类的构造函数中:
JSON.stringify({a:new String(document.getElementById('id').value)}); -> {"a":""}
Run Code Online (Sandbox Code Playgroud)
我找不到主要问题,但是有了这个,它在 Internet Explorer 和 FireFox 中都运行良好。
我对这个肮脏的解决方案不太满意,但付出的努力并不多。
JSON 库:https://github.com/douglascrockford/JSON-js/blob/master/json2.js