当前位置: 首页 > news >正文

JSON入门略要

JavaScript对象表示法(JavaScript Object Notation,JSON)已经成为RESTful接口设计中的事实标准。

JSON数据格式使得应用程序可以通过RESTful API等方式在网络上进行数据通信。
REST: 表现层状态转化(REpresentation State Transfer)
同样由对象、数组、名称-值 结构体组成。JSON是一种技术标准。

JSON 示例1-1 firstValidObject.json
{“thisIs”: “My first JSON document”}

“thisIs”为名称,其值为My first JSON document”

JSON 示例1-2 firstValidArray.json
[
“also”,
“a”,
“valid”,JSON,
“doc”
]

名称-值对:数据属性和值的一组对应
对象:名称-值对的无序集合
数组:值的有序集合
名称-值对示例

JSON 示例1-3 nameValue.json
[
“conference”:OSCON,
“speechTitle”:JSON at Work”,
“track”: “Web APIs”
]

每一个键名(如conference”)是一个字符串,必须由双引号括起来。
“OSCON”是值,值的类型有很多。
对象示例:

JSON 示例1-4 simpleJsonObject.json
{
“address”: {
“line1”:555 Any Street”,
“city”:”Denver”,
“stateOrProvince”:CO,
“zipOrPostalcode”:80202,
“country”:USA}
}

带有内嵌数组的对象:

JSON 示例1-5 jsonObjectNestedArray.json
{
“speaker”: {
“firstName”: “Larson”,
“lastName”:”Richard”,
“topics”: [JSON,REST,SOA]
}
}

内嵌其他对象的对象:

JSON 示例1-5 jsonObjectNestedArray.json
{
“speaker”: {
“firstName”: “Larson”,
“lastName”:”Richard”,
“topics”: [JSON,REST,SOA]
}
}

内嵌其他对象和数组的数组示例:

JSON 示例1-7 jsonArray.json
{
“presentations”: [
{
“title”:JSON at Work:  Overview and Ecosystem”,
“length”:90 minutes”,
“abstract”: [JSON ks more than just a simple replacement for XML when”, ”you make an AJAX call.],
“track”:”Web APIs”
},
{
“title”: ”RESTful Security at Work”,
“length”:90 minutes”,
“abstract”: [ “You’ve been working with RESTful Web Services for a few years”, “now, and you’d like to know if your  services are secure.],
“track”: “Web APIs”
}
]
}

null:并不是一种值的类型,而是JSON中一种特殊值。null不由引号 括起来,表示某个键或属性没有值,用作占位符。

日期属性的值:

JSON 示例1-8 jsonDateFormat.json
{
“dateRegustered”:2014-03-01T23:46:11-05:00}

JSON.stringify()和JSON.parse()进行序列化/反序列化操作,将外部信息转换成自身可理解的数据结构
JSON Schema是对JSON文档/消息中的内容、结构与格式的声明。JSON Schema可以校验JSON文档,进行语义校验。
JSON Schema声明示例:

JSON 示例1-9 ex-1-basic-schema.json
{
“$schema”: ”http://json-schema.org/draft-04/schema#”,
“type”: “object”,
“properties”: {
“email”: {
“type”: “string”
},
“firstName”:{
“type”: “string”
},
“lastName”:{
“type”: “string”
}
}
}

与上述Schema对应的JSON实例

JSON 示例1-10 ex-1-basic.json
{
“email”: “larsonrichard@ecratic.com”,
“firstName”:“Larson”,
“lastName”: “Richard”
}

禁止JSON中出现额外字段:
“additionalProperties”: false
确保JSON中包含所有的必须字段:
“required”: [“email”, “firstName”, “lastName”, “postedSlides”, “rating”]
使用JSON SChema来校验数组

JSON 示例1-11 basic-types-validation-req-schema.json
{
“$schema”: ”http://json-schema.org/draft-04/schema#”,
“type”: “object”,
“properties”: {
“tags”: {
“type”: “array”,
“items”: {
“type”: “string”
}
} 
},
“additionalProperties”: false,
“required”: [“tags”]
}

非法示例,tags数组中不能包含整数,无法通过校验:

JSON 示例1-12 array-simple-invalid.json
{
“tags”: [“fred”,1] 
}

通过使用patternProperties关键词,JSON schema中的模式属性可以基于正则表达式来声明部分重复的字段名。

JSON 示例1-11 basic-types-validation-req-schema.json
{
“$schema”: ”http://json-schema.org/draft-04/schema#”,
“type”: “object”,
“properties”: {
“city”: {
“type”: “string”
},
“state”: {
“type”: “string”
},“zip”:{
“type”: “string”
},
“country”: {
“type”: “string”
}
},
“patternProperties”:{^line[1-3]$”:{
“type”: “string”
}
},
“additionalProperties”: false,
“required”: [“city”, “state”, “zip”, “country”, “line1”]
}

以上示例,正则表达式^line[1-3]$允许JSON文档中出现以下地址字段line1、line2、line3,其中:
【^】表示字符串开头
【line】表示字符串”line”
[1-3]表示1至3之间的一个整数
$表示字符串结尾

http://www.lryc.cn/news/538189.html

相关文章:

  • Python爬虫抓取数据时,如何设置请求头?
  • 以若依移动端版为基础,实现uniapp的flowable流程管理
  • DeepSeek 助力 Vue 开发:打造丝滑的开关切换(Switch)
  • unity学习39:连续动作之间的切换,用按键控制角色的移动
  • C++ ——构造函数
  • Python实现语音识别详细教程【2025】最新教程
  • 【第12章:深度学习与伦理、隐私—12.4 深度学习与伦理、隐私领域的未来挑战与应对策略】
  • Django中数据库迁移命令
  • Win11 远程 连接 Ubuntu20.04(局域网)
  • 安卓手游内存call综合工具/内部call/安卓注入call/数据分析(类人猿学院)
  • PPT工具集
  • SpringBoot:使用spring-boot-test对web应用做单元测试时如何测试Filter?
  • 解锁 Java 回调函数:异步编程与事件处理的利器
  • 记PasteSpider部署工具的Windows.IIS版本开发过程之草稿-Web.IIS.Administration解读(5)
  • MySQL Workbench安装教程以及菜单汉化
  • 【ISO 14229-1:2023 UDS诊断全量测试用例清单系列:第十节】
  • Python的imutils库详细介绍
  • 常用查找算法整理(顺序查找、二分查找、插值查找、斐波那契查找、哈希查找、二叉排序树查找、平衡二叉树查找、红黑树查找、B树和B+树查找、分块查找)
  • 2526考研资料分享 百度网盘
  • 网络编程(24)——实现带参数的http-get请求
  • 东方财富股吧发帖与评论爬虫
  • 【Elasticsearch】match_bool_prefix查询
  • 微信小程序image组件mode属性详解
  • 数据结构:最小生成树
  • C语言-章节 4:函数的定义与声明 ——「神秘法术的卷轴」
  • 《云原生安全攻防》-- K8s镜像安全:镜像全生命周期安全管理
  • uniapp商城之首页模块
  • 【Javascript Day13、14、15、16】
  • linux 板子的wifi模块连上路由器后,用udhcpc给板子wifi分配ip,udhcpc获取到ip,但没有写入wlan0网卡上
  • openGauss 3.0 数据库在线实训课程13: 学习逻辑结构:表管理1