当前位置:网站首页>JS基础12
JS基础12
2022-04-22 12:23:00 【鹏程933】
JSON
定义:是一种轻量级的数据组织格式和数据交换格式,它基于JS语言一个子集
语法
- 简单值:字符串,数值,布尔值,null可以在JSON中出现,undefined不可以
- 对象:第一种复杂数据类型,对象表示有序键值对
- 数组:第二种复杂数据类型,数组表示可以通过数值索引访问
注:JSON没有变量,函数,对象实例概念
JSON语法规则
//对象
{
"user":{
"username":"jack"
"age":"18"
}
}
//数组
["jack",18]
JSON存在形式
- 两种形式
JSON字符串形式
let jsonStr='{"name","jack"}'
JSON对象<=>JS简单对象
let jsonObj={name:"jack"} - JSON两种形式准换
JSON对象=>JSON.stringify()=>JSON字符串
JSON字符串=>JSON.parse()=>JSON对象
序列化选项
JSON.stringify()除了要序列化对象,还可接受两个参数
- 第一个参数过滤器
- 如果参数是数组,返回结果只会包含该数组列出对象的属性
- 如果参数是函数,可接受两个参数(属性名key和属性值value)
<script> let book={
title:"js",
authors:[
"jack",
"rose"
],
num:1,
year:2000
}
//传入数组
let jsonBook=JSON.stringify(book,["title","num"])
console.log(jsonBook);
//结果{
"title":"js","num":1}
//传入函数 let jsonBook2=JSON.stringify(book,(key,value)=>{
switch(key){
case "title":
return value
case "year":
return 5000;
default:
return value
}
})
console.log(jsonBook2);
//结果{
"title":"js","authors":["jack","rose"],"num":1,"year":5000}
</script>
- 字符串缩进
JSON.stringify()第三个参数控制缩进和空格
let book={
title:"js",
authors:[
"jack",
"rose"
],
num:1,
year:2000
}
let jsonBook3=JSON.stringify(book,null,"--")
console.log(jsonBook3);
//结果{
--"title": "js",
--"authors": [
----"jack",
----"rose"
--],
--"num": 1,
--"year": 2000
}
toJSON()方法,该方法与date.toISOString()本质上一样
let book2={
title:"js", authors:[ "jack", "rose" ], num:1, year:2000, date:"1996-01-01T00:00:00+08:00", toJSON:function(){
return this.date
}
}
let jsonBook4=JSON.stringify(book2)
console.log(jsonBook4);
//结果"1996-01-01T00:00:00+08:00"
解析选项
还原函数JSON.parse()如果返回undefined,就会删除相应的键,如果是其他值,该值就会成为相应的键插入结果中
let book={
title:"js",
authors:[
"jack",
"rose"
],
num:1,
year:2000,
date:new Date(2022,1,1)
}
let jsonBook=JSON.stringify(book)
let newJsonbook=JSON.parse(jsonBook,(key,value)=>
key=="date"?new Date(value):value
)
console.log(newJsonbook.date.getFullYear());
//结果2022
持久化存储技术
- 持久化存储
硬盘=>持久化存储
内存=>临时存储 - 前端:localStorage
系统自动创建,直接使用
| 方法 | 含义 |
|---|---|
| localStorage.setItem(‘username’,‘jack’) | 设置存储名为username值为jack |
| let value=localStorage.getItem(‘username’) | 获取存储名为username的值 |
| locaStorage.removeItem(‘username’) | 移出存储名为username |
注:保存位置与域名有关
版权声明
本文为[鹏程933]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_64925940/article/details/123852297
边栏推荐
- [concurrent programming 050] type and description of memory barrier?
- What are the main mobile camera chips?
- 【keras入门】MNIST数据集分类
- JS 【詳解】作用域
- 栈的概念和操作
- LeetCode_DFS_中等_200.岛屿数量
- Setting policy of thread pool size
- [concurrent programming 055] will the following daemon thread execute the code in the finally module?
- 【并发编程054】多线程的状态及转换过程?
- [in depth understanding of tcallusdb technology] description of data interface for batch deletion of specified location in list - [list table]
猜你喜欢
随机推荐
When the new version of redis is released, do you still think redis is a single thread?
What are the main mobile camera chips?
[concurrent programming 050] type and description of memory barrier?
案例4-1.7:文件傳輸(並查集)
Efr32 crystal calibration guide
机器学习 训练模板,汇总多个分类器
有研究显示,现在年轻人越来越不愿意换手机了。下一代智能手机在硬件上出现哪些更新,才会让你有换机的冲动?
UML summary
Interpretation of tamigou project | 49.5% equity transfer of Beijing Hualong pawn Co., Ltd
Where have all the Internet people who left their jobs gone?
CPU和GPU有什么区别?
可以测量50A以上电流的隔离集成式电流传感器CH704应用案例分享
订货系统打破批发企业瓶颈期,助力企业数字化转型
Electrician Lecture 2
[in depth understanding of tcallusdb technology] description of data interface for batch deletion of specified location in list - [list table]
栈的概念和操作
论文阅读《Attention Concatenation Volume for Accurate and Efficient Stereo Matching》
STM32F429BIT6 SD卡模拟U盘
build 知:Makefile
购买不同品牌的手机,怎么对比硬件配置?









