JSON 简介

JSON 简介

一句话

JSON 是一种用纯文本表示结构化数据的格式,用来在程序之间传数据,或把数据存到文件里。它不属于任何语言,Python、Java、C++、浏览器都能读写。

全称 JavaScript Object Notation,名字里的 JavaScript 只是出典(2001 年 Douglas Crockford 从 JS 的对象字面量语法里提取出来),现在标准是 RFC 8259 / ECMA-404,与 JavaScript 已无绑定关系。

长什么样

json 复制代码
{
  "name": "旭",
  "age": 24,
  "tags": ["C++", "后端"],
  "meta": { "vip": true, "note": null }
}

结构上是一棵树:整份文档本身就是一个值 ,最常见的是对象 {}。
#mermaid-svg-3KLNTOMcuSeJa8gG{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-3KLNTOMcuSeJa8gG .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-3KLNTOMcuSeJa8gG .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-3KLNTOMcuSeJa8gG .error-icon{fill:#552222;}#mermaid-svg-3KLNTOMcuSeJa8gG .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-3KLNTOMcuSeJa8gG .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-3KLNTOMcuSeJa8gG .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-3KLNTOMcuSeJa8gG .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-3KLNTOMcuSeJa8gG .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-3KLNTOMcuSeJa8gG .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-3KLNTOMcuSeJa8gG .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-3KLNTOMcuSeJa8gG .marker{fill:#333333;stroke:#333333;}#mermaid-svg-3KLNTOMcuSeJa8gG .marker.cross{stroke:#333333;}#mermaid-svg-3KLNTOMcuSeJa8gG svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-3KLNTOMcuSeJa8gG p{margin:0;}#mermaid-svg-3KLNTOMcuSeJa8gG .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-3KLNTOMcuSeJa8gG .cluster-label text{fill:#333;}#mermaid-svg-3KLNTOMcuSeJa8gG .cluster-label span{color:#333;}#mermaid-svg-3KLNTOMcuSeJa8gG .cluster-label span p{background-color:transparent;}#mermaid-svg-3KLNTOMcuSeJa8gG .label text,#mermaid-svg-3KLNTOMcuSeJa8gG span{fill:#333;color:#333;}#mermaid-svg-3KLNTOMcuSeJa8gG .node rect,#mermaid-svg-3KLNTOMcuSeJa8gG .node circle,#mermaid-svg-3KLNTOMcuSeJa8gG .node ellipse,#mermaid-svg-3KLNTOMcuSeJa8gG .node polygon,#mermaid-svg-3KLNTOMcuSeJa8gG .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-3KLNTOMcuSeJa8gG .rough-node .label text,#mermaid-svg-3KLNTOMcuSeJa8gG .node .label text,#mermaid-svg-3KLNTOMcuSeJa8gG .image-shape .label,#mermaid-svg-3KLNTOMcuSeJa8gG .icon-shape .label{text-anchor:middle;}#mermaid-svg-3KLNTOMcuSeJa8gG .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-3KLNTOMcuSeJa8gG .rough-node .label,#mermaid-svg-3KLNTOMcuSeJa8gG .node .label,#mermaid-svg-3KLNTOMcuSeJa8gG .image-shape .label,#mermaid-svg-3KLNTOMcuSeJa8gG .icon-shape .label{text-align:center;}#mermaid-svg-3KLNTOMcuSeJa8gG .node.clickable{cursor:pointer;}#mermaid-svg-3KLNTOMcuSeJa8gG .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-3KLNTOMcuSeJa8gG .arrowheadPath{fill:#333333;}#mermaid-svg-3KLNTOMcuSeJa8gG .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-3KLNTOMcuSeJa8gG .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-3KLNTOMcuSeJa8gG .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-3KLNTOMcuSeJa8gG .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-3KLNTOMcuSeJa8gG .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-3KLNTOMcuSeJa8gG .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-3KLNTOMcuSeJa8gG .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-3KLNTOMcuSeJa8gG .cluster text{fill:#333;}#mermaid-svg-3KLNTOMcuSeJa8gG .cluster span{color:#333;}#mermaid-svg-3KLNTOMcuSeJa8gG div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-3KLNTOMcuSeJa8gG .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-3KLNTOMcuSeJa8gG rect.text{fill:none;stroke-width:0;}#mermaid-svg-3KLNTOMcuSeJa8gG .icon-shape,#mermaid-svg-3KLNTOMcuSeJa8gG .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-3KLNTOMcuSeJa8gG .icon-shape p,#mermaid-svg-3KLNTOMcuSeJa8gG .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-3KLNTOMcuSeJa8gG .icon-shape .label rect,#mermaid-svg-3KLNTOMcuSeJa8gG .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-3KLNTOMcuSeJa8gG .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-3KLNTOMcuSeJa8gG .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-3KLNTOMcuSeJa8gG :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 整份文档 = 对象 { }
字符串

name: "旭"
数字

age: 24
数组

tags: ...
对象

meta: {...}
布尔

vip: true

六种值,没有第七种

类型 写法 说明
字符串 "你好" 只能用双引号,单引号非法
数字 42、-3.14、1e9 不区分整数与浮点,没有 NaN、Infinity
布尔 true / false 小写
空值 null 是值类型,不是"未定义"
数组 [1, "a", null] 有序,元素类型可混合
对象 {"k": 1} 键必须是双引号字符串

前四种是标量,后两种是容器。容器里可以无限嵌套,所以 JSON 能表达任意结构。

五条铁律

  1. 键与字符串一律双引号 ,{'a':1}、{a:1} 都不合法。
  2. 不允许注释 ,写配置要说明只能靠 "_comment" 这类约定字段。
  3. 最后一个元素后不能有逗号 ,[1,2,] 直接语法错误。
  4. 没有日期、没有二进制、没有函数 。日期约定写 ISO 8601 字符串 "2026-10-23T15:28:52+08:00",二进制走 Base64。
  5. 整数别超过 2^53。JSON 数字在多数语言里落到 IEEE 754 double,19 位雪花 ID 要序列化成字符串再传,否则静默丢精度。

为什么用它

#mermaid-svg-s6PFXt8k0GuZdzXK{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-s6PFXt8k0GuZdzXK .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-s6PFXt8k0GuZdzXK .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-s6PFXt8k0GuZdzXK .error-icon{fill:#552222;}#mermaid-svg-s6PFXt8k0GuZdzXK .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-s6PFXt8k0GuZdzXK .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-s6PFXt8k0GuZdzXK .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-s6PFXt8k0GuZdzXK .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-s6PFXt8k0GuZdzXK .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-s6PFXt8k0GuZdzXK .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-s6PFXt8k0GuZdzXK .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-s6PFXt8k0GuZdzXK .marker{fill:#333333;stroke:#333333;}#mermaid-svg-s6PFXt8k0GuZdzXK .marker.cross{stroke:#333333;}#mermaid-svg-s6PFXt8k0GuZdzXK svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-s6PFXt8k0GuZdzXK p{margin:0;}#mermaid-svg-s6PFXt8k0GuZdzXK .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-s6PFXt8k0GuZdzXK .cluster-label text{fill:#333;}#mermaid-svg-s6PFXt8k0GuZdzXK .cluster-label span{color:#333;}#mermaid-svg-s6PFXt8k0GuZdzXK .cluster-label span p{background-color:transparent;}#mermaid-svg-s6PFXt8k0GuZdzXK .label text,#mermaid-svg-s6PFXt8k0GuZdzXK span{fill:#333;color:#333;}#mermaid-svg-s6PFXt8k0GuZdzXK .node rect,#mermaid-svg-s6PFXt8k0GuZdzXK .node circle,#mermaid-svg-s6PFXt8k0GuZdzXK .node ellipse,#mermaid-svg-s6PFXt8k0GuZdzXK .node polygon,#mermaid-svg-s6PFXt8k0GuZdzXK .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-s6PFXt8k0GuZdzXK .rough-node .label text,#mermaid-svg-s6PFXt8k0GuZdzXK .node .label text,#mermaid-svg-s6PFXt8k0GuZdzXK .image-shape .label,#mermaid-svg-s6PFXt8k0GuZdzXK .icon-shape .label{text-anchor:middle;}#mermaid-svg-s6PFXt8k0GuZdzXK .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-s6PFXt8k0GuZdzXK .rough-node .label,#mermaid-svg-s6PFXt8k0GuZdzXK .node .label,#mermaid-svg-s6PFXt8k0GuZdzXK .image-shape .label,#mermaid-svg-s6PFXt8k0GuZdzXK .icon-shape .label{text-align:center;}#mermaid-svg-s6PFXt8k0GuZdzXK .node.clickable{cursor:pointer;}#mermaid-svg-s6PFXt8k0GuZdzXK .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-s6PFXt8k0GuZdzXK .arrowheadPath{fill:#333333;}#mermaid-svg-s6PFXt8k0GuZdzXK .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-s6PFXt8k0GuZdzXK .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-s6PFXt8k0GuZdzXK .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-s6PFXt8k0GuZdzXK .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-s6PFXt8k0GuZdzXK .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-s6PFXt8k0GuZdzXK .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-s6PFXt8k0GuZdzXK .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-s6PFXt8k0GuZdzXK .cluster text{fill:#333;}#mermaid-svg-s6PFXt8k0GuZdzXK .cluster span{color:#333;}#mermaid-svg-s6PFXt8k0GuZdzXK div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-s6PFXt8k0GuZdzXK .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-s6PFXt8k0GuZdzXK rect.text{fill:none;stroke-width:0;}#mermaid-svg-s6PFXt8k0GuZdzXK .icon-shape,#mermaid-svg-s6PFXt8k0GuZdzXK .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-s6PFXt8k0GuZdzXK .icon-shape p,#mermaid-svg-s6PFXt8k0GuZdzXK .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-s6PFXt8k0GuZdzXK .icon-shape .label rect,#mermaid-svg-s6PFXt8k0GuZdzXK .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-s6PFXt8k0GuZdzXK .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-s6PFXt8k0GuZdzXK .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-s6PFXt8k0GuZdzXK :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 序列化 serialize
HTTP / 文件 / 日志
反序列化 parse
内存对象

dict / struct
JSON 文本
JSON 文本
另一端的内存对象

语言 序列化 反序列化
Python json.dumps(obj) json.loads(text)
JavaScript JSON.stringify(obj) JSON.parse(text)
C++(nlohmann) j.dump() json::parse(text)
Java(Jackson) writeValueAsString() readValue()

带 s 的是字符串版,不带的 dump / load 要配文件对象用。

一个高频混淆点:JSON ≠ JS 对象字面量

JSON JS 对象字面量
键的引号 必须双引号 可省略、可单引号
注释 禁止 允许
尾逗号 禁止 允许
值 只有 6 种 可含函数、undefined、Date

JSON.parse 只认严格 JSON,{a:1} 会被拒。

邻近格式

  • YAML:有注释、对人友好,K8s 与 CI 配置常用;缩进敏感是祖传坑。
  • XML:冗余、体积大,但有属性、命名空间、XSD 校验,老系统与企业协议里仍常见。
  • MessagePack / Protobuf:二进制,体积小 30%~70%、解析快,但不可直接读。内部服务间调用多用,对外 API 还是 JSON。

总结

JSON 用 6 种值类型加两对括号,凑出一个所有语言都能理解的通用结构描述。好处是人能读、机器能解析、对接零成本;代价是类型贫瘠(没日期、整数丢精度)且冗余(键名反复出现)。数据量大或性能敏感时,才需要换二进制协议。

相关推荐
xbzb2 天前
镜像到底存在哪:/var/lib/docker 的四级映射与 daemon.json 配置
docker·容器·json·镜像·daemon.json·四级
坤坤子吖2 天前
Linux网络编程:自定义应用层协议与 JSON 序列化
linux·服务器·网络·c++·笔记·json
code2cat3 天前
【随笔】结构化输出:JSON格式过关,业务数据就能入库吗
json·大语言模型
Hello_Pyhx6 天前
NWinfo 导出 Windows 硬件信息:从 JSON 报告到缺项排查
windows·json
程序员大阳7 天前
副队长JavaScript教程(66)-本地存储实战:待办事项网页的开发
javascript·json·实战·待办事项·本地存储·案例
视觉小萌新8 天前
AI Class 3 · Chapter 2-3:函数、异常、文件读写+JSON / JSONL 读写(小白版)
人工智能·python·json
翊众建站9 天前
用 JSON-LD 给企业官网做结构化数据:LocalBusiness 与 FAQPage 实践笔记
人工智能·搜索引擎·json
Patrick_Wilson9 天前
读懂一个 Go JSON 处理函数:从输入数据追踪结构体、校验与错误返回
go·json·编程语言
January120710 天前
【AI方法笔记】2026-11-02:模型流式吐 JSON 时,前端会遇到四种崩法
前端·人工智能·笔记·json