JSON书写格式
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json

·
JSON是什么
JSON ( JavaScript Object Notation) ,是一种数据交互格式。
为什么有这个技术
Json之前,大家都用 XML 传递数据。XML 是一种纯文本格式,所以适合在网络上交换数据,但是 XML 格式比较复杂,终于道格拉斯·克罗克福特(Douglas Crockford)发明了JSON 这种超轻量级的数据交换格式。
JSON 如何使用
- 数据格式
JSON 实际上是 JavaScript 的一个子集,所以JSON 的数据格式和 JavaScript 是对应的:
- number => JS number
- boolean => JS boolean
- string => JS string //
- null => JS null
- array => JS Array 的表达方式 []
- object => JS {} 表达式
- 注意事项
- JSON 规定字符集是UTF-8,字符串必须使用
""
,Object 的键也必须使用""
- 数组或对象的最后一个成员,不能加 逗号
- JS 内置两个Json方法
JSON.stringify( {} , [ ] , "")
//参数一 :要序列化的数据(object)
//参数二 :控制对象的键值,只想输出指定的属性,传入一个数组
//参数三 :序列化后,打印输出的格式(一个Tab ,可以更直观查看json)
任何把 JavaScript 变成 Json ,就是把这个对象序列化为Json字符串,然后才可以通过网络传递;
收到一个Json格式的字符串,
JSON.parse( json.DATA ) //传入json字符串
如果我们收到一个JSON格式的字符串,只需要把它反序列化成一个JavaScript对象,就可以在JavaScript中直接使用这个对象了。
实例:
1.数组方式 [ ]
[{
"id" : 1 ,
"name" : "xiaoming"
},{
"id" : 2 ,
"name" : "xiaohong"
}]
2.对象方式 { }
//前后端分离,推荐后端返回给前端数据格式
{
"status" : 0 , //执行状态码
"msg" : "SUCCESS", //说明文字信息,没有为NULL
"data" :[{ //对象中嵌套数组,数组是返回的数据,
"id" : 1 ,
"name" : "xiaohong"
},{
"id" : 2,
"name" : "xiaoming"
}]
}
3.反例
{"id" : ox16 } //不合法,数值需要是十进制
{"name" : underfined } //不合法,没有该值
[{
"name" : NUll,
"school" : function() {
console.log("该写法是错误的")
}//不合法
}]//json中不能使用自定义函数,或系统内置函数




适用于现代 C++ 的 JSON。
最近提交(Master分支:4 个月前 )
f06604fc
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com>
---------
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> 28 天前
d23291ba
* add a ci step for Json_Diagnostic_Positions
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* Update ci.cmake to address review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* address review comment
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix typo in the comment
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix typos in ci.cmake
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* invoke the new ci step from ubuntu.yml
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* issue4561 - use diagnostic positions for exceptions
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_documentation check
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* address review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci check failures for unit-diagnostic-postions.cpp
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* improvements based on review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix const correctness string
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* further refinements based on reviews
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* add one more test case for full coverage
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* ci check fix - add const
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* add unit tests for json_diagnostic_postions only
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_diagnostics
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_build_documentation check
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
---------
Signed-off-by: Harinath Nampally <harinath922@gmail.com> 28 天前
更多推荐
所有评论(0)