我有这个json对象:
values: {
username: { type: String, unique: true },
password: { type: String }
}当运行JSON.stringify(values)时,我得到:
{"username":{"unique":true},"password":{}}它省略了type: String。
有什么解决方法吗?
发布于 2012-11-29 10:33:48
如果你真的想这样做,你可以使用replacer参数(虽然不确定这将是如何跨浏览器的)并解决这个问题。这个函数基本上会检查该值是否为函数,并返回函数名。
JSON.stringify(your_object, function (key, value) {
if (typeof value === "function") {
return value.name; // or value.toString() for the function body/declaration
}
return value;
});尽管,为了清楚起见,最好使用字符串作为类型("String"而不是String),这样就没有什么可跳过的了。
发布于 2012-11-29 10:26:32
尝试包括引号:
values: {
username: { type: "String", unique: true },
password: { type: "String" }
}https://stackoverflow.com/questions/13617847
复制相似问题