日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

JSON.stringify的使用

發布時間:2023/12/10 javascript 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JSON.stringify的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

JSON.stringify 詳細使用

官方定義

interface JSON {/*** Converts a JavaScript Object Notation (JSON) string into an object.* @param text A valid JSON string.* @param reviver A function that transforms the results. This function is called for each member of the object.* If a member contains nested objects, the nested objects are transformed before the parent object is.*/parse(text: string, reviver?: (this: any, key: string, value: any) => any): any;/*** Converts a JavaScript value to a JavaScript Object Notation (JSON) string.* @param value A JavaScript value, usually an object or array, to be converted.* @param replacer A function that transforms the results.* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.*/stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string;/*** Converts a JavaScript value to a JavaScript Object Notation (JSON) string.* @param value A JavaScript value, usually an object or array, to be converted.* @param replacer An array of strings and numbers that acts as a approved list for selecting the object properties that will be stringified.* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.*/stringify(value: any, replacer?: (number | string)[] | null, space?: string | number): string; }

stringify 那些類型可顯示

class stringifyVisualble {a = '123';b = () => {};c = true;d = null;e = new Set();f = new Map();g = undefined;h = NaN;i = Symbol('1212');j = {k: 1}constructor() {this.e.add(1);this.f.set('z', 1);} } let stringifyVisualbleInstance = new stringifyVisualble(); let stringifyVisualbleStr = JSON.stringify(stringifyVisualbleInstance); console.log(stringifyVisualbleStr);

打印結果為

{"a":"123","c":true,"d":null,"e":{},"f":{},"h":null,"j":{"k":1}}

重寫toJSON

class TOJSON {a = 1;toJSON() {return { b: 2};} } console.log(JSON.stringify(new TOJSON()))

打印結果

{"b":2}

stringify 的 replacer的使用

stringify(value: any, replacer?: (number | string)[] | null, space?: string | number): string;

class Replacer{a = 1;b = 2;c = {a: 3,d: 4} } console.log(JSON.stringify(new Replacer(), ['a','c']));

打印結果

{"a":1,"c":{"a":3}}

stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string;

class Replacer{a = 1;b = 2;c = {a: 3,d: 4} } console.log('結果',(<any>JSON.stringify)(new Replacer(), (target: any, key: any, value: any) => {console.log(target)console.log(key);console.log(value);let b = '1';return 'res'; }));

打印結果很奇怪,和lib.es5.d.ts中的定義不一致,還需要再了解了解

Replacer { a: 1, b: 2, c: { a: 3, d: 4 } } undefined 結果 "res"

space

class Replacer{a = 1;b = 2;c = {a: 3,d: 4} } console.log(JSON.stringify(new Replacer(), undefined, '-'))

打印結果

{ -"a": 1, -"b": 2, -"c": { --"a": 3, --"d": 4 -} }

可以使用制表符’\t’,實現換行

總結

以上是生活随笔為你收集整理的JSON.stringify的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。