JSON序列化与反序列化
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json
·
一.JSON语法

重点:
1.简单值里面不包括undefined !!!
2.JSON字符串必须使用双引号,单引号会导致语法错误。
3.JSON与JS对象的不同之处:JSON不用声明变量,不用分号结束,属性名必须用双引号包裹。
二,JSON序列化与反序列化
1.JSON.stringify()
const info = {
name: 'lx',
age: 24,
hobby: ['tv', 'shopping']
}
const jsonText = JSON.stringify(info)
console.log(jsonText) // {"name":"lx","age":24,"hobby":["tv","shopping"]}
JSON.stringify()的第二个参数,相当于过滤器,可以是数组或者函数,数组里有什么就返回什么。
const info = {
name: 'lx',
age: 24,
hobby: ['tv', 'shopping']
}
// 数组
const jsonText2 = JSON.stringify(info, ['name', 'hobby'])
console.log(jsonText2) // {"name":"lx","hobby":["tv","shopping"]}
// 函数
const jsonText3 = JSON.stringify(info, (key, value) => {
switch (key) {
case 'name':
return 'lxlx'
case 'hobby':
return value.join()
case 'age':
return undefined
default:
return value
}
})
console.log(jsonText3) // {"name":"lxlx","hobby":"tv,shopping"}当值为undefined时不显示
JSON.stringify()的第三个参数用来控制缩进,第三个参数可以是数值或者字符串,且不大于10,数值大于10,当成0来处理,字符串大于10,只截取前10位
const info = {
name: 'lx',
age: 24,
hobby: ['tv', 'shopping']
}
const jsonText3 = JSON.stringify(info, null, 8)
console.log(jsonText3)
{
"name": "lx",
"age": 24,
"hobby": [
"tv",
"shopping"
]
}
const jsonText4 = JSON.stringify(info, null, '--')
console.log(jsonText4)
{
--"name": "lx",
--"age": 24,
--"hobby": [
----"tv",
----"shopping"
--]
}
适用于现代 C++ 的 JSON。
最近提交(Master分支:5 个月前 )
34665ae6
binary -> binary_t
Signed-off-by: Robert Chisholm <robert.chisholm@sheffield.ac.uk> 20 天前
f3dc4684
Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.28.9 to 3.28.10.
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](https://github.com/github/codeql-action/compare/9e8d0789d4a0fa9ceb6b1738f7e269594bdd67f0...b56ba49b26e50535fa1e7f7db0f4f7b4bf65d80d)
---
updated-dependencies:
- dependency-name: github/codeql-action
dependency-type: direct:production
update-type: version-update:semver-patch
...
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> 26 天前
更多推荐




所有评论(0)