关于 JavaScript的JSON的一些小技巧

* 1、格式化

默认的字符串化器还会缩小 JSON,看起来很难看

  1. const user = {
  2. name: 'John',
  3. age: 30,
  4. isAdmin: true,
  5. friends: ['Bob', 'Jane'],
  6. address: {
  7. city: 'New York',
  8. country: 'USA'
  9. }
  10. };
  11. console.log(JSON.stringify(user));
  12. //=> {"name":"John","age":30,"isAdmin":true,"friends":["Bob","Jane"],"address":{"city":"New York","country":"USA"}}

JSON.stringify也有一个内置的格式化程序!

  1. console.log(JSON.stringify(user, null, 2));
  2. // {
  3. // "name": "John",
  4. // "age": 30,
  5. // "isAdmin": true,
  6. // "friends": [
  7. // "Bob",
  8. // "Jane"
  9. // ],
  10. // "address": {
  11. // "city": "New York",
  12. // "country": "USA"
  13. // }
  14. // }

(如果你想知道那个 null 是什么,我们稍后会谈到)
在此示例中,JSON 格式为 2 个缩进空格。
我们还可以指定用于缩进的自定义字符。

  1. console.log(JSON.stringify(user, null, 'lol'));
  2. // {
  3. // lol"name": "John",
  4. // lol"age": 30,
  5. // lol"isAdmin": true,
  6. // lol"friends": [
  7. // lollol"Bob",
  8. // lollol"Jane"
  9. // lol],
  10. // lol"address": {
  11. // lollol"city": "New York",
  12. // lollol"country": "USA"
  13. // lol}
  14. // }

* 2、隐藏字符串化数据中的某些属性

JSON.stringify第二个参数,这在很大程度上是未知的。它被称为replacer,它是一个函数或数组,用于决定哪些数据保留在输出中,哪些不保留。
这是一个简单的示例,我们可以在其中隐藏password用户。

  1. const user = {
  2. name: 'John',
  3. password: '12345',
  4. age: 30
  5. };
  6. console.log(JSON.stringify(user, (key, value) => {
  7. if (key === 'password') {
  8. return;
  9. }
  10. return value;
  11. }));

这是输出:
{“name”:”John”,”age”:30}
我们可以进一步重构:

  1. function stripKeys(...keys) {
  2. return (key, value) => {
  3. if (keys.includes(key)) {
  4. return;
  5. }
  6. return value;
  7. };
  8. }
  9. const user = {
  10. name: 'John',
  11. password: '12345',
  12. age: 30,
  13. gender: 'male'
  14. };
  15. console.log(JSON.stringify(user, stripKeys('password', 'gender')))

输出:
{“name”:”John”,”age”:30}
还可以传递一个数组来仅获取某些键:

  1. const user = {
  2. name: 'John',
  3. password: '12345',
  4. age: 30
  5. }
  6. console.log(JSON.stringify(user, ['name', 'age']))

输出相同的东西。
这也适用于数组。如果你有一大堆蛋糕:

  1. const cakes = [
  2. {
  3. name: 'Chocolate Cake',
  4. recipe: [
  5. 'Mix flour, sugar, cocoa powder, baking powder, eggs, vanilla, and butter',
  6. 'Mix in milk',
  7. 'Bake at 350 degrees for 1 hour',
  8. // ...
  9. ],
  10. ingredients: ['flour', 'sugar', 'cocoa powder', 'baking powder', 'eggs', 'vanilla', 'butter']
  11. },
  12. // tons of these
  13. ];

我们可以轻松地做同样的事情,并且替换器将应用于每个蛋糕:

  1. const cakes = [
  2. {
  3. name: 'Chocolate Cake',
  4. recipe: [
  5. 'Mix flour, sugar, cocoa powder, baking powder, eggs, vanilla, and butter',
  6. 'Mix in milk',
  7. 'Bake at 350 degrees for 1 hour',
  8. // ...
  9. ],
  10. ingredients: ['flour', 'sugar', 'cocoa powder', 'baking powder', 'eggs', 'vanilla', 'butter']
  11. },
  12. // tons of these
  13. ];
  14. console.log(JSON.stringify(cakes, ['name']))

我们得到这个:
[{“name”:”Chocolate Cake”},{“name”:”Vanilla Cake”},…]

3、使用toJSON创建自定义输出格式

如果一个对象实现了该toJSON函数,JSON.stringify将使用它来对数据进行字符串化。
考虑一下:

  1. class Fraction {
  2. constructor(n, d) {
  3. this.numerator = n;
  4. this.denominator = d;
  5. }
  6. }
  7. console.log(JSON.stringify(new Fraction(1, 2)))

这将输出{“numerator”:1,”denominator”:2}. 但是如果我们想用一个字符串替换它1/2呢?
进入toJSON

  1. class Fraction {
  2. constructor(n, d) {
  3. this.numerator = n;
  4. this.denominator = d;
  5. }
  6. toJSON() {
  7. return `${this.numerator}/${this.denominator}`
  8. }
  9. }
  10. console.log(JSON.stringify(new Fraction(1, 2)))

JSON.stringify尊重toJSON财产和产出”1/2”。

4、恢复数据

我们上面的分数示例效果很好。但是如果我们想恢复数据呢?当我们再次解析 JSON 时,如果分数能神奇地返回,那不是很酷吗?我们可以!
进入复活者!

  1. class Fraction {
  2. constructor(n, d) {
  3. this.numerator = n;
  4. this.denominator = d;
  5. }
  6. toJSON() {
  7. return `${this.numerator}/${this.denominator}`
  8. }
  9. static fromJSON(key, value) {
  10. if (typeof value === 'string') {
  11. const parts = value.split('/').map(Number);
  12. if (parts.length === 2) return new Fraction(parts);
  13. }
  14. return value;
  15. }
  16. }
  17. const fraction = new Fraction(1, 2);
  18. const stringified = JSON.stringify(fraction);
  19. console.log(stringified);
  20. // "1/2"
  21. const revived = JSON.parse(stringified, Fraction.fromJSON);
  22. console.log(revived);
  23. // Fraction { numerator: 1, denominator: 2 }

我们可以传递第二个参数JSON.parse来指定 reviver 函数。恢复器的工作是将字符串化数据“恢复”回其原始形式。在这里,我们传递了一个 reviver,它是类的静态fromJSON属性Fraction。
在这种情况下,reviver 检查该值是否是一个有效的分数,如果是,它会创建一个新Fraction对象并返回它。
有趣的事实:此功能用于内置的 Date 对象。尝试查找Date.prototype.toJSON
这就是为什么它有效:

  1. console.log(JSON.stringify(new Date()))
  2. //=> '"2022-03-01T06:28:41.308Z"'

要恢复日期,我们可以使用JSON.parse:

  1. function reviveDate(key, value) {
  2. const regex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,}|)Z$/;
  3. if (typeof value === "string" && regex.test(value)) {
  4. return new Date(value);
  5. }
  6. return value;
  7. }
  8. console.log(JSON.parse('"2022-03-01T06:28:41.308Z"', reviveDate))
  9. //=> Tue Mar 01 2022 06:28:41 GMT-0700 (Pacific Daylight Time)

5、使用revivers隐藏数据

与解析器一样,恢复器也可用于隐藏数据。它以相同的方式工作。
这是一个例子:

  1. const user = JSON.stringify({
  2. name: 'John',
  3. password: '12345',
  4. age: 30
  5. });
  6. console.log(JSON.parse(user, (key, value) => {
  7. if (key === 'password') {
  8. return;
  9. }
  10. return value;
  11. }));

这是输出:
{ name: ‘John’, age: 30 }