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的使用相关推荐

  1. JSON.stringify()

    写在前边 不言而喻,JSON.stringify() 是用来将合法的JSON数据字符串化的!然而在正常的工作中我们用到的只是最基础的功能:今天我们就探索不一样的JSON.stringify(). 基础 ...

  2. json.parse()和json.stringify()

    json.parse() 用于从一个字符串解析出json对象 var str = '{"name":"huangzhong","age":& ...

  3. JSON.stringify报cyclic object value错误

    这是一个典型的循环引用的错误,一个对象里引用自己就会立刻得到这个错误: obj = { x:555, y: "hi" }; obj.myself = obj;try{json = ...

  4. (转)JS之——解决IE6、7、8使用JSON.stringify报JSON未定义错误的问题

    https://blog.csdn.net/l1028386804/article/details/53439755 在通过JavaScript将对象类型的参数通过JSON.stringify转换成字 ...

  5. ajax 时间格式string,ajax 数据请求:json格式在浏览器变成了string ,使用JSON.stringify(params)方法...

    var params = { md5str: "sf", datastr: "sf", } var ajaxRequest = $.ajax({ url: ur ...

  6. 理解JSON对象:JSON.parse、 JSON.stringify

    何时是JSON,何时不是JSON? JSON就是一个有特殊规则的字符串,按照这个规则我们就可以把这个字符串解析成JS对象. JSON是设计成描述数据交换格式的,他也有自己的语法,这个语法是JavaSc ...

  7. js中JSON.stringify用于自定义的类

    参考:http://stackoverflow.com/questions/7356694/how-to-json-stringify-a-user-defined-class-in-javascri ...

  8. 理解JSON.stringify()高级用法

    一:JSON.stringify() 该方法是把javascript对象转换成json字符串. 基本语法:JSON.stringify(value, [, replacer], [, space]) ...

  9. JSON.stringify()和JSON.parse()分别是什么

    JSON.stringify() 从一个对象中解析出字符串 JSON.stringify({"a":"1","b":"2" ...

  10. javascript 数组和对象的浅复制和深度复制 assign/slice/concat/JSON.parse(JSON.stringify())...

    javascript 数组和对象的浅度复制和深度复制 在平常我们用 '='来用一个变量引用一个数组或对象,这里是'引用'而不是复制下面我们看一个例子引用和复制是什么概念 var arr=[1,2,3, ...

最新文章

  1. BMC:幼年特发性关节炎患儿肠道菌群的特征、生物标记的识别及其在临床预测中的作用...
  2. 常用组策略故障排除工具
  3. C#异常--System.IO.FileLoadException:“混合模式程序集是针对“v2.0.50727”版的运行时生成的错误...
  4. 五行中的土在哪个方位_土命人适合往哪个方向发展
  5. 2018-07-10 为Chrome和火狐浏览器编写扩展
  6. 史上最全的 MySQL 高性能优化实战总结
  7. Java StackTraceElement toString()方法与示例
  8. java水泡_JAVA图像处理系列(八)——艺术效果:水泡
  9. android 属性动画实例,Android 属性动画Animator工具类代码案例
  10. win11内存泄露怎么办 Windows11内存泄露的解决方法
  11. springboot + mybatis + 多数据源
  12. rpm 完全卸载mysql
  13. Delphi 有关的网址
  14. 2018-《此生未完成》于娟
  15. 关于移动MM购买的相关资料
  16. 「经济/商学/理财」简说
  17. 益智app游戏 android,儿童宝宝益智游戏
  18. Android学习日记(yzy):SQLite数据库和baseAdapter
  19. mysql 减号与下划线_匹本中文、字母、数字、下划线、减号的正则表达式,这样写为什么不对?...
  20. 用IDEA新建一个工程项目并编写HelloWorld

热门文章

  1. (Kinetisnbsp;K60)WDOG看门狗测试
  2. unity 插件uniwebview 内嵌H5游戏
  3. win10开始菜单添加磁贴_Windows 10开始菜单磁贴美化教程
  4. 反编译工具Reflector下载(集成两个常用.net插件,FileGenerator和FileDisassembler)
  5. BackTrack4 官方指南
  6. backtrack5 oracle,BackTrack5(BT5)硬盘安装
  7. Python + OpenCv实现视频中的车辆检测计数(车流量统计) (2020.7.15已更新源码)
  8. PDG转PDF全攻略
  9. 基于netty构建http服务器
  10. oracle10g 64位安装包下载,ORACLE 10g 64位下载地址