快速入门 JSON 数据格式
<h2 style="color: black; text-align: left; margin-bottom: 10px;">引言</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">JSON,全<span style="color: black;">叫作</span> <strong style="color: blue;">J</strong>ava<strong style="color: blue;">S</strong>cript <strong style="color: blue;">O</strong>bject <strong style="color: blue;">N</strong>otion,类似于XML,YAML,Properties等,是一种数据交换格式,相比于XML,更简单,更轻量,<span style="color: black;">更易</span>理解。</p><img src="https://mmbiz.qpic.cn/mmbiz_png/YQoul0gm36DEhqBe8NF7icKrmLyL3ME0j4Pk9l9S1YeJ0wicCwHXqXiaUFMV5KKyxice6uiaznHDBqumvq3dgALIs3A/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;">JSON vs XML<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">运用</span></h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">JSON <span style="color: black;">日前</span>被广泛<span style="color: black;">做为</span> Web <span style="color: black;">研发</span>中前后端数据的交换格式(尤其是 Rest API 中)。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">例如百度:</p><img src="https://mmbiz.qpic.cn/mmbiz_png/YQoul0gm36DEhqBe8NF7icKrmLyL3ME0jerbXPhwYf0icOLNk951XIBwcJ0KWgicUySqg49yEHFupZUxncHqqUJUg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;">百度<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">例如b站:</p><img src="https://mmbiz.qpic.cn/mmbiz_png/YQoul0gm36DEhqBe8NF7icKrmLyL3ME0jJpibnpncMkibdCnVhZWvR9hhRpukL323BynuOVM83L32iaOtszh9rqPwg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;">bilibili<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">除此以外,JSON 还经常<span style="color: black;">做为</span>配置文件的格式。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">如 VsCode的配置文件:</p><img src="https://mmbiz.qpic.cn/mmbiz_png/YQoul0gm36DEhqBe8NF7icKrmLyL3ME0jIficTUTEslKhDuAt1mBGkTUkywOX7A0pMv2pqjORSPjP6DTrM3gtaUQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;">VScode Settings<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">如 Eslint 的配置文件:</p><img src="https://mmbiz.qpic.cn/mmbiz_png/YQoul0gm36DEhqBe8NF7icKrmLyL3ME0ju4hTFzYwYMzfaxD5pdh8GsTn1iasYNvtx83icuVUPZzxic0d7ALg4fuhQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;">Eslint Settings<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">JetBrains <span style="color: black;">机构</span>新发布的 Fleet IDE <span style="color: black;">亦</span><span style="color: black;">运用</span> JSON <span style="color: black;">做为</span>配置文件:</p><img src="https://mmbiz.qpic.cn/mmbiz_png/YQoul0gm36DEhqBe8NF7icKrmLyL3ME0jicwWzJiaFQapHmojEDqEYdCQRPVIevribANaZ1tib7gL3MPT1XNCCg3iaJA/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;">Fleet Settings<h2 style="color: black; text-align: left; margin-bottom: 10px;">优点</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">总体<span style="color: black;">来讲</span>,JSON<span style="color: black;">拥有</span>两个优点:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">第1</span>,语法<span style="color: black;">非常</span>简单,如下面这个例子:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">"name"</span>: <span style="color: black;">"古月有三木"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p> <span style="color: black;">"university"</span>: <span style="color: black;">"whu"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p> <span style="color: black;">"gender"</span>: <span style="color: black;">"男"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p> <span style="color: black;">"isProgrammer"</span>: <span style="color: black;">true</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">不论是<span style="color: black;">研发</span>者还是<span style="color: black;">运用</span>者,都很容易理解这个信息。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">同期</span>,又<span style="color: black;">由于</span> JSON 是 Javascript 的子集,<span style="color: black;">因此</span>前端<span style="color: black;">研发</span>者几乎<span style="color: black;">能够</span>无痛上手,这<span style="color: black;">亦</span>是 JSON 被广泛用于前后端数据交换的<span style="color: black;">原由</span>之一。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">第二,得益于 JSON 的简洁,相<span style="color: black;">针对</span> XML,编写一个 JSON 解析器就容易的多,事实上,<span style="color: black;">日前</span>几乎所有流行的编程语言都<span style="color: black;">供给</span>了对 JSON 的解析。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">数据类型</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">JSON 支持以下几种数据类型:</p>String,字符串类型,强制为 UTF-8 编码Number,数值类型,支持整数、浮点数、科学计数法Boolean,布尔类型,true/falseNull,空值Array,数组,数组的值<span style="color: black;">能够</span>为 String, Number, Boolean, Null, Array <span style="color: black;">或</span> ObjectObject,对象类型,对象的键必须为字符串,值<span style="color: black;">能够</span>为六种类型之一。<h3 style="color: black; text-align: left; margin-bottom: 10px;">Object</h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Object 是键值对的集合,格式为:</p><img src="https://mmbiz.qpic.cn/mmbiz_png/YQoul0gm36DEhqBe8NF7icKrmLyL3ME0jhHOOtbBbZnYNZqFibVQnRvgHcNlXT3C7lgtGREudIicy7MTV0v4kdVlA/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;">Object, from json.org<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">其中键必须为字符串,值<span style="color: black;">能够</span>为六种类型之一,<span style="color: black;">重视</span>,键和值之间需要<span style="color: black;">运用</span> : 隔开,<span style="color: black;">区别</span>键值对之间需要<span style="color: black;">运用</span> , 隔开,但最后一个键值对后<span style="color: black;">不可</span>加 , 。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;">Array</h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Array 是值的有序列表,格式如下:</p><img src="https://mmbiz.qpic.cn/mmbiz_png/YQoul0gm36DEhqBe8NF7icKrmLyL3ME0j8J5UMV2rdYIYibOTPBTIVjqPZyrfx6OcFc4lydrAVxuh2b3gKzAVGpQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;">Array, from json.org<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">其类似于编程语言中的数组,值<span style="color: black;">能够</span>为六种类型之一,<span style="color: black;">区别</span>值之间需要<span style="color: black;">运用</span> , 隔开,但最后一个值后面<span style="color: black;">不可</span>添加 ,。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">实质</span><span style="color: black;">运用</span></h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">上文<span style="color: black;">已然</span><span style="color: black;">说到</span>,<span style="color: black;">日前</span>大<span style="color: black;">都数</span>主流语言均<span style="color: black;">供给</span>对 JSON 的解析,下面<span style="color: black;">咱们</span>看几个例子。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;">JavaScript</h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">JavaScript <span style="color: black;">供给</span>了对 JSON 的原生解析<span style="color: black;">办法</span>。</p><span style="color: black;">const</span> jsonStr = <span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">`</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> "name": "Civitasv",</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> "university": "Whu",</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> "age": 22,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> "gender": "M",</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> "isProgrammer": true</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>`
</span><span style="color: black;">const</span> jsonObject = <span style="color: black;">JSON</span>.parse(jsonStr)<span style="color: black;">// <span style="color: black;">运用</span> Json.parse 解析 Json 字符串</span><span style="color: black;">Object</span>.keys(jsonObject).forEach(<span style="color: black;"><span style="color: black;">item</span> =></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {</p> <span style="color: black;">console</span>.log(item, <span style="color: black;">": "</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">, jsonObject)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">})</p><span style="color: black;">console</span>.log(<span style="color: black;">"=============================="</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)</p><span style="color: black;">const</span> toJsonStr = <span style="color: black;">JSON</span>.stringify(jsonObject) <span style="color: black;">// <span style="color: black;">运用</span> Json.stringfy 将 json 对象输出为 json 字符串</span><span style="color: black;">console</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.log(toJsonStr)</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;">Python</h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Python 中的 json 库<span style="color: black;">供给</span>了对 JSON 的解析<span style="color: black;">办法</span>。</p><span style="color: black;">import</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> json</p>jsonStr = <span style="color: black;">""</span><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">"</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> "
</span>name<span style="color: black;">": "</span>Civitasv<span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">",</p> "
</span>university<span style="color: black;">": "</span>Whu<span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">",</p> "
</span>age<span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">": 22,</p> "
</span>gender<span style="color: black;">": "</span>M<span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">",</p> "
</span>isProgrammer<span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">": true</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>"
</span><span style="color: black;">""</span>jsonObject = json.loads(jsonStr)<span style="color: black;">// <span style="color: black;">运用</span> json.loads 将 json 字符串解析为 python 对象</span><span style="color: black;">for</span> item <span style="color: black;">in</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> jsonObject:</p> print(<span style="color: black;">"{}: {}"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.format(item, jsonObject))</p>print(<span style="color: black;">"=============================="</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)</p>toJsonStr = json.dumps(jsonObject, ensure_ascii=False)<span style="color: black;">// <span style="color: black;">运用</span> json.dumps 将 python 对象解析为 json 字符串</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">print(toJsonStr)</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;">C++</h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">C++ 中有许多的 JSON 解析库,<span style="color: black;">运用</span> nlohmann 的 json 库介绍如下。</p><span style="color: black;">#<span style="color: black;">include</span> <span style="color: black;">"json.hpp"</span></span><span style="color: black;">#<span style="color: black;">include</span> <span style="color: black;"><string></span></span><span style="color: black;">#<span style="color: black;">include</span> <span style="color: black;"><iostream></span></span><span style="color: black;">using</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> json = nlohmann::json;</p><span style="color: black;"><span style="color: black;">int</span> <span style="color: black;">main</span><span style="color: black;">()</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">std</span>::<span style="color: black;">string</span> jsonStr = <span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">R"(</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> "name": "Civitasv",</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">"university": "Whu",</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> "age": 22,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> "gender": "M",</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> "isProgrammer": true</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> )"
</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p> json jsonObject = json::parse(jsonStr); <span style="color: black;">// <span style="color: black;">运用</span> json::parse 解析 json 字符串</span> <span style="color: black;">for</span> (<span style="color: black;">auto</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">& : jsonObject.items())</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {</p> <span style="color: black;">std</span>::<span style="color: black;">cout</span> << key << <span style="color: black;">": "</span> << value << <span style="color: black;">"\n"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;">std</span>::<span style="color: black;">cout</span> << <span style="color: black;">"=============================="</span> << <span style="color: black;">\n</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p> <span style="color: black;">std</span>::<span style="color: black;">string</span> toJsonStr = jsonObject.dump(); <span style="color: black;">// 使用 dump <span style="color: black;">办法</span>将 json 对象解析为字符串</span> <span style="color: black;">std</span>::<span style="color: black;">cout</span> << toJsonStr << <span style="color: black;">\n</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">结语</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">以上是对 JSON 数据格式以及<span style="color: black;">运用</span><span style="color: black;">办法</span>的介绍,本质上说,JSON 只是<span style="color: black;">拥有</span><span style="color: black;">必定</span>格式的字符串,但编写方和<span style="color: black;">运用</span>方遵循相同的规范,就<span style="color: black;">能够</span>实现数据的交换了,<span style="color: black;">因此</span>,JSON 解析器就<span style="color: black;">显出</span><span style="color: black;">非常</span>重要,我将在<span style="color: black;">将来</span>的<span style="color: black;">文案</span>中<span style="color: black;">详细</span>介绍<span style="color: black;">怎样</span><span style="color: black;">运用</span> C++ 编写一个基本的 JSON 解析器,代码我<span style="color: black;">已然</span>完<span style="color: black;">成为了</span>,<span style="color: black;">位置</span>为<span style="color: black;">A Tiny Json Parser</span> ,对此感兴趣的<span style="color: black;">能够</span>先看一下代码。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">点个关注不迷路。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">参考资料</span></h3><span style="color: black;"><span style="color: black;"></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">A Tiny Json Parser: https://github.com/Civitasv/mini-json-parser</p>
</span>
期待楼主的下一次分享!” 顶楼主,说得太好了! 软文发布论坛开幕式圆满成功。 http://www.fok120.com 你字句如珍珠,我珍藏这份情。
页:
[1]