在执行JSON.stringify(hash)时如何保留未定义的值?
以下是一个例子:
var hash = {
"name" : "boda",
"email" : undefined,
"country" : "africa"
};
var string = JSON.stringify(hash);
> "{"name":"boda","country":"africa"}"
电子邮件从JSON.stringify中消失。
答案 0 :(得分:76)
您可以将替换器功能传递给JSON.stringify
,以自动将undefined
值转换为null
值,如下所示:
var string = JSON.stringify(
obj,
function(k, v) { return v === undefined ? null : v; }
);
这适用于数组内部的未定义值,因为JSON.stringify
已将这些值转换为null
。
答案 1 :(得分:9)
使用null
代替undefined
。
var hash = {
"name" : "boda",
"email" : null,
"country" : "africa"
};
var string = JSON.stringify(hash);
> "{"name":"boda","email":null,"country":"africa"}"
答案 2 :(得分:4)
这应该可以解决问题
from sklearn import tree
modelDTC_Dog = tree.DecisionTreeClassifier(criterion='gini')
modelDTC_Dog.fit(df_Dog_X, df_Dog_y)
predictedDTC_Dog = modelDTC_Dog.predict(df_Dog_X)
from sklearn.linear_model import LogisticRegression
logreg = LogisticRegression()
logreg.fit(df_Dog_X, df_Dog_y)
logreg_predict = logreg.predict(df_Dog_X)
from sklearn.ensemble import RandomForestClassifier
modelRFC = RandomForestClassifier(n_estimators=100, criterion='entropy')
modelRFC.fit(df_Dog_X, df_Dog_y)
predictedRFC = modelRFC.predict(df_Dog_X)
答案 3 :(得分:2)
您可以通过将其转换为null来保存它;
在ES6中:
JSON.stringify(obj, (k, v) => v === undefined ? null : v)
旧版浏览器
JSON.stringify(obj, function(k,v){return v===undefined ? null : v;})
答案 4 :(得分:1)
我在这里阅读这些行并猜测你想在使用JSON.parse时未定义值吗?
如果是这种情况,您可以使用以下内容:
var encodeUndefined = function(obj, undefinedPaths, path) {
path = path || 'ROOT';
for (var key in obj) {
var keyPath = path + '.' + key;
var value = obj[key];
if (value === undefined) {
undefinedPaths.push(keyPath);
} else if (typeof value == "object" && value !== null) {
encodeUndefined(obj[key], undefinedPaths, keyPath);
}
}
}
var stringifyAndPreserveUndefined = function(obj) {
var undefinedPaths = [];
//save all paths that have are undefined in a array.
encodeUndefined((obj), undefinedPaths);
return JSON.stringify({
ROOT: obj,
undefinedPaths: undefinedPaths
}, function(k, v) { if (v === undefined) { return null; } return v; });
}
var parseAndRestoreUndefined = function(value) {
var data = JSON.parse(value);
var undefinedPaths = data.undefinedPaths;
var obj = data.ROOT;
//Restore all undefined values
for (var pathIndex = 0; pathIndex < undefinedPaths.length; pathIndex++) {
var pathParts = undefinedPaths[pathIndex].substring(5).split('.');
var item = obj;
for (var pathPartIndex = 0; pathPartIndex < pathParts.length - 1; pathPartIndex++) {
item = item[pathParts[pathPartIndex]];
}
item[pathParts[pathParts.length - 1]] = undefined;
}
return obj;
}
var input = {
test1: 'a',
test2: 'b',
test3: undefined,
test4: {
test1: 'a',
test2: undefined
}
};
var result = stringifyAndPreserveUndefined(input);
var result2 = parseAndRestoreUndefined(result);
stringifyAndPreserveUndefined
会对数组中所有未定义的值进行编码,当您调用parseAndRestoreUndefined
时,它会将它们重新放在正确的位置。
一个缺点是json看起来不像对象。在上面的示例中,它将变为{"ROOT":{"test1":"a","test2":"b","test4":{"test1":"a"}},"undefinedPaths":["ROOT.test3","ROOT.test4.test2"]}
答案 5 :(得分:1)
function stringifyWithUndefined(obj, space) {
const str = JSON.stringify(obj, (key, value) => value === undefined ? '__undefined__' : value, space);
return str.replace(/"__undefined__"/g, 'undefined');
}
示例:
const obj = {
name: 'boda',
email: undefined,
country: 'africa'
};
console.log(stringifyWithUndefined(obj, 2));
结果:
{
"name": "boda",
"email": undefined,
"country": "africa"
}
答案 6 :(得分:0)
这将导致它打印为Component.onCompleted: {
MyType.myList = [
MyListElement {
}
]
}
,但这是INVALID json,但是有效的JavaScript。
undefined