惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
N
News and Events Feed by Topic
AI
AI
S
Secure Thoughts
Schneier on Security
Schneier on Security
Help Net Security
Help Net Security
N
News | PayPal Newsroom
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Hacker News: Ask HN
Hacker News: Ask HN
W
WeLiveSecurity
Google Online Security Blog
Google Online Security Blog
T
Tailwind CSS Blog
Jina AI
Jina AI
小众软件
小众软件
S
Security @ Cisco Blogs
A
About on SuperTechFans
雷峰网
雷峰网
T
Threat Research - Cisco Blogs
I
InfoQ
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
AWS News Blog
AWS News Blog
The Register - Security
The Register - Security
V
Visual Studio Blog
PCI Perspectives
PCI Perspectives
Blog — PlanetScale
Blog — PlanetScale
L
LINUX DO - 最新话题
Stack Overflow Blog
Stack Overflow Blog
MongoDB | Blog
MongoDB | Blog
博客园 - 叶小钗
P
Proofpoint News Feed
美团技术团队
F
Fortinet All Blogs
NISL@THU
NISL@THU
T
Troy Hunt's Blog
U
Unit 42
博客园 - Franky
B
Blog
Webroot Blog
Webroot Blog
T
The Exploit Database - CXSecurity.com
The Hacker News
The Hacker News
宝玉的分享
宝玉的分享
Y
Y Combinator Blog
The Cloudflare Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Google DeepMind News
Google DeepMind News
P
Privacy & Cybersecurity Law Blog
Latest news
Latest news
C
Cyber Attacks, Cyber Crime and Cyber Security
GbyAI
GbyAI

博客园 - 前端架构师

Claude Code 源码架构分析 我不是狐狸,我是那Harness Engineering 小龙虾的自我养成之路 vue3+ts添加公共富文本组件 vue watch详解 微信授权没搞过? css3图片防止变形 公众号使用微信sdk的正确姿势 web离线应用前提之离线检测 es6手写一个call 同学给我来个手写的new吧 this综合篇 this绑定番外篇之箭头函数绑定 vue中引入公用过滤器? this详解下 012天this详解上 011天之跨域资源共享CORS 009天之跨浏览器的事件处理程序 使用XHR上传文件要不要了解一下?
010天JSON.stringify()详解
前端架构师 · 2020-03-22 · via 博客园 - 前端架构师

JSON.stringify()除了要序列化的js对象外,还可以接受另外两个参数,这两个参数用于指定以不同的方式序列化js对象。第一个参数是个过滤器,可以是一个数组,也可以是一个函数;第二个参数是一个选项,表示是否在JSON字符串中保留缩紧。单独或组合使用这两个参数,可以更全面深入地控制JSON的序列化。

1.过滤结果

如果过滤器参数是数组,那么JSON.stringify()的结果中将只包含数组中列出的属性。

let book = {
    'title':'profession',
    'authors':['yk'],
    edition:3,
    year:2020
}

let jsonText = JSON.stringify(book,['title','edition']);

得到的jsonText为:

{'title':'profession',edition:3}

看下面一个传入函数的例子

let book = {
    'title':'profession',
    'authors':['yk'],
    edition:3,
    year:2020
}

let jsonText = JSON.stringify(book,function(key, value){
    switch(key) {
        case "authors":
            return value.join(",")
        
        case "year":
            return 5000;
            
        case "edition":
            return undefined;
            
        default:
            return value;
        
    }
});

函数的时候传入两个,key和value,属性名key只能是字符串,如果函数返回undefined,那么相应的属性会被忽略。

所以上面得到的结果是

{'title':'profession','authors':'yk',year:5000}

二。JSON.stringify的字符串缩进功能

JSON.stringify()方法的第三个参数用于控制结果中的缩进和空白符。如果这个参数是一个数值,那么它表示的是每个级别缩进的空格数。例如要在每个级别缩进4个空格:

let book = {
    'title':'profession',
    'authors':[
        'yk'
    ],
    edition:3,
    year:2020
}

let jsonText = JSON.stringify(book,null,4);

得到的jsonText是:

let book = {
        'title':'profession',
        'authors':[
            'yk'
        ],
        edition:3,
        year:2020
}
⚠这个数值️最大不能超过10,超过10的按照10处理。


有意思的来了,如果缩进参数是一个字符串而非数值,那么这个字符串会被当作缩进符(不再用空格)
let jsonText = JSON.stringify(book,null,"--");

得到jsontext

{
--'title':'profession',
--'authors':[
----'yk'
--],
--edition:3,
--year:2020
}

同样缩紧字符不能超过10个,超过10个,结果也只会出现前10个字符

扫码加群,每日更新一篇前端技术文章一起成长。