自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

分享五個(gè)使用 JSON 相關(guān)方法的小技巧

開發(fā) 前端
本文介紹一些關(guān)于 JavaScript 的 JSON 的一些小技巧,希望對大家有所幫助!

1、格式化

默認(rèn)的字符串化器還會縮小 JSON,看起來很難看

const user = {
name: 'John',
age: 30,
isAdmin: true,
friends: ['Bob', 'Jane'],
address: {
city: 'New York',
country: 'USA'
}
};
console.log(JSON.stringify(user));
//=> {"name":"John","age":30,"isAdmin":true,"friends":["Bob","Jane"],"address":{"city":"New York","country":"USA"}}

JSON.stringify也有一個(gè)內(nèi)置的格式化程序!

console.log(JSON.stringify(user, null, 2));
// {
// "name": "John",
// "age": 30,
// "isAdmin": true,
// "friends": [
// "Bob",
// "Jane"
// ],
// "address": {
// "city": "New York",
// "country": "USA"
// }
// }

(如果你想知道那個(gè) null 是什么,我們稍后會談到)

在此示例中,JSON 格式為 2 個(gè)縮進(jìn)空格。

我們還可以指定用于縮進(jìn)的自定義字符。

console.log(JSON.stringify(user, null, 'lol'));
// {
// lol"name": "John",
// lol"age": 30,
// lol"isAdmin": true,
// lol"friends": [
// lollol"Bob",
// lollol"Jane"
// lol],
// lol"address": {
// lollol"city": "New York",
// lollol"country": "USA"
// lol}
// }

2、隱藏字符串化數(shù)據(jù)中的某些屬性

JSON.stringify第二個(gè)參數(shù),這在很大程度上是未知的。它被稱為replacer,它是一個(gè)函數(shù)或數(shù)組,用于決定哪些數(shù)據(jù)保留在輸出中,哪些不保留。

這是一個(gè)簡單的示例,我們可以在其中隱藏password用戶。

const user = {
name: 'John',
password: '12345',
age: 30
};

console.log(JSON.stringify(user, (key, value) => {
if (key === 'password') {
return;
}

return value;
}));

這是輸出:

{"name":"John","age":30}

我們可以進(jìn)一步重構(gòu):

function stripKeys(...keys) {
return (key, value) => {
if (keys.includes(key)) {
return;
}

return value;
};
}

const user = {
name: 'John',
password: '12345',
age: 30,
gender: 'male'
};

console.log(JSON.stringify(user, stripKeys('password', 'gender')))

輸出:

{"name":"John","age":30}

還可以傳遞一個(gè)數(shù)組來僅獲取某些鍵:

const user = {
name: 'John',
password: '12345',
age: 30
}

console.log(JSON.stringify(user, ['name', 'age']))

輸出相同的東西。

這也適用于數(shù)組。如果你有一大堆蛋糕:

const cakes = [
{
name: 'Chocolate Cake',
recipe: [
'Mix flour, sugar, cocoa powder, baking powder, eggs, vanilla, and butter',
'Mix in milk',
'Bake at 350 degrees for 1 hour',
// ...
],
ingredients: ['flour', 'sugar', 'cocoa powder', 'baking powder', 'eggs', 'vanilla', 'butter']
},
// tons of these
];

我們可以輕松地做同樣的事情,并且替換器將應(yīng)用于每個(gè)蛋糕:

const cakes = [
{
name: 'Chocolate Cake',
recipe: [
'Mix flour, sugar, cocoa powder, baking powder, eggs, vanilla, and butter',
'Mix in milk',
'Bake at 350 degrees for 1 hour',
// ...
],
ingredients: ['flour', 'sugar', 'cocoa powder', 'baking powder', 'eggs', 'vanilla', 'butter']
},
// tons of these
];

console.log(JSON.stringify(cakes, ['name']))

我們得到這個(gè):

[{"name":"Chocolate Cake"},{"name":"Vanilla Cake"},...]

3、使用toJSON創(chuàng)建自定義輸出格式

如果一個(gè)對象實(shí)現(xiàn)了該toJSON函數(shù),JSON.stringify將使用它來對數(shù)據(jù)進(jìn)行字符串化。

考慮一下:

class Fraction {
constructor(n, d) {
this.numerator = n;
this.denominator = d;
}
}

console.log(JSON.stringify(new Fraction(1, 2)))

這將輸出{"numerator":1,"denominator":2}. 但是如果我們想用一個(gè)字符串替換它1/2呢?

進(jìn)入toJSON

class Fraction {
constructor(n, d) {
this.numerator = n;
this.denominator = d;
}

toJSON() {
return `${this.numerator}/${this.denominator}`
}
}

console.log(JSON.stringify(new Fraction(1, 2)))

JSON.stringify尊重toJSON財(cái)產(chǎn)和產(chǎn)出"1/2"。

4、恢復(fù)數(shù)據(jù)

我們上面的分?jǐn)?shù)示例效果很好。但是如果我們想恢復(fù)數(shù)據(jù)呢?當(dāng)我們再次解析 JSON 時(shí),如果分?jǐn)?shù)能神奇地返回,那不是很酷嗎?我們可以!

進(jìn)入復(fù)活者!

class Fraction {
constructor(n, d) {
this.numerator = n;
this.denominator = d;
}

toJSON() {
return `${this.numerator}/${this.denominator}`
}

static fromJSON(key, value) {
if (typeof value === 'string') {
const parts = value.split('/').map(Number);
if (parts.length === 2) return new Fraction(parts);
}

return value;
}
}

const fraction = new Fraction(1, 2);
const stringified = JSON.stringify(fraction);
console.log(stringified);

// "1/2"
const revived = JSON.parse(stringified, Fraction.fromJSON);
console.log(revived);
// Fraction { numerator: 1, denominator: 2 }

我們可以傳遞第二個(gè)參數(shù)JSON.parse來指定 reviver 函數(shù)?;謴?fù)器的工作是將字符串化數(shù)據(jù)“恢復(fù)”回其原始形式。在這里,我們傳遞了一個(gè) reviver,它是類的靜態(tài)fromJSON屬性Fraction。

在這種情況下,reviver 檢查該值是否是一個(gè)有效的分?jǐn)?shù),如果是,它會創(chuàng)建一個(gè)新Fraction對象并返回它。

有趣的事實(shí):此功能用于內(nèi)置的 Date 對象。嘗試查找Date.prototype.toJSON

這就是為什么它有效:

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

要恢復(fù)日期,我們可以使用JSON.parse:

function reviveDate(key, value) {
const regex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,}|)Z$/;

if (typeof value === "string" && regex.test(value)) {
return new Date(value);
}

return value;
}
console.log(JSON.parse('"2022-03-01T06:28:41.308Z"', reviveDate))
//=> Tue Mar 01 2022 06:28:41 GMT-0700 (Pacific Daylight Time)

5、使用revivers隱藏?cái)?shù)據(jù)

與解析器一樣,恢復(fù)器也可用于隱藏?cái)?shù)據(jù)。它以相同的方式工作。

這是一個(gè)例子:

const user = JSON.stringify({
name: 'John',
password: '12345',
age: 30
});

console.log(JSON.parse(user, (key, value) => {
if (key === 'password') {
return;
}

return value;
}));

這是輸出:

{ name: 'John', age: 30 }

責(zé)任編輯:龐桂玉 來源: 前端大全
相關(guān)推薦

2010-11-12 10:13:46

數(shù)據(jù)中心改造

2010-02-24 11:22:04

WCF方法重載

2023-11-05 19:46:56

JavaIntelliJ代碼

2022-06-21 09:02:49

python技巧

2023-10-26 18:03:14

索引Python技巧

2022-07-15 15:30:13

Python技巧

2010-09-13 16:13:47

DIV CSS表單

2011-06-16 17:54:25

關(guān)鍵詞

2011-05-10 17:06:05

SEO

2022-05-24 14:07:53

OpenFeignSpring開源

2011-11-22 13:55:42

遠(yuǎn)程管理網(wǎng)絡(luò)管理

2010-01-07 17:41:19

JSON定義法

2011-03-11 16:25:53

Oracle數(shù)據(jù)庫

2015-08-17 15:53:58

Linux桌面

2024-01-03 08:53:35

JavaScrip編程語言NodeJS

2011-07-15 17:35:19

JavaScript

2012-09-11 14:55:29

Moosefs

2015-07-27 09:36:09

storyboard

2022-04-02 09:56:44

pipPython

2011-06-28 13:56:43

JAVA
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號