JSON空字符串 [英] JSON empty string

查看:803
本文介绍了JSON空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么JSON.stringify-Function将string.Empty(")转换为"null" -String? 问题,为什么我不使用:

why does the JSON.stringify-Function converts a string.Empty ("") to a "null"-String? The problem, why i'm not using:

JSON.parse(json, function(key, value) {
    if (typeof value === 'string') {
        if (value == 'null')
            return '';
        return value;
    }
});

...是的,如果有人真的写"null"(不太可能,但有可能),我有问题...

...is, if somebody really write "null" (is very unlikely, but possible), i have a problem to...

感谢您的回答!

推荐答案

旧问题-但当您搜索"json字符串化空字符串"时,它是最主要的结果,所以我将分享我找到的答案.

Old question - but its the top result when you search for 'json stringify empty string' so I'll share the answer I found.

在某些版本的IE8中,这似乎是一个错误,其中空的DOM元素返回的值看起来像一个空字符串,与空字符串相比评估为true,但实际上有一些不同的编码表示它为null值.

This appears to be a bug in certain versions of IE8, where empty DOM elements return a value which looks like an empty string, evaluates true when compared to an empty string, but actually has some different encoding denoting that it is a null value.

一种解决方案是每当调用stringify时都进行替换.

One solution is to do a replace whenever you call stringify.

JSON.stringify(foo, function(key, value) { return value === "" ? "" : value });

另请参见 查看全文

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