【发布时间】:2021-10-20 02:10:32
【问题描述】:
我使用JSON.stringify(value, replacer) 来屏蔽 JSON 对象中的敏感数据。
const inputJsonObject =
{
"id": "uniqueId",
"value": "objectData",
"user": {
"password": "qwerty"
}
};
const hideDataMethod = JSON.stringify(inputJsonObject, (key, value) => {
const dataToHide = ['id', 'user.password'];
return dataToHide.indexOf(key) === -1 ? value : 'xxx';
});
console.log(hideDataMethod);
如何获得user.password 值?上述解决方案适用于id,但不适用于密码。
【问题讨论】:
-
为什么是 JSON?只需遍历对象并直接替换值。有很多问题可以解决如何做到这一点。
-
这能回答你的问题吗? Dynamically change nested JSON
-
因为密码是
password而不是user.password -
请注意,您所称的
inputJsonObject不是 JSON,它只是一个对象。 JSON 是一种用于数据交换的文本表示法。 (More here.) 如果您正在处理 JavaScript 源代码,而不是处理 string,那么您就不是在处理 JSON。 (同样,hideDataMethod不是方法,而是字符串。)
标签: javascript json object stringify