当前位置: 首页 > 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之间的一个整数
$表示字符串结尾

相关文章:

  • Lua 数据库访问
  • 30个常用的DEEPSEEK提示词
  • “单击以重新设置PIN”的解决方案
  • 算法-链表篇04-两两交换链表中的节点
  • DeepSeek告别服务器繁忙
  • C++游戏开发流程图
  • 基于Spring Boot+Vue的宠物服务管理系统(源码+文档)
  • Java里ArrayList和LinkedList有什么区别?
  • python的pass
  • 基于Python的深度学习音乐推荐系统(有配套论文)
  • MySQL8.x版本的新的功能特性总结
  • 【AI论文】随机鹦鹉在大型语言模型(LLM)之肩:物理概念理解的总结性评估
  • STM32物联网终端实战:从传感器到云端的低功耗设计
  • 【etcd】etcd_go操作与etcd锁实现原理
  • jQuery介绍(快速、简洁JavaScript库,诞生于2006年,主要目标是简化HTML文档操作、事件处理、动画和Ajax交互)
  • ai生成毕业论文(ai写作论文免费网站推荐)
  • Hot100 堆
  • 每日一题——把数字翻译成字符串
  • unity学习43:子状态机 sub-state machine
  • 【数据标准】企业的数据标准化从业务、技术、管理视角的要求
  • 习近平出席中国-拉美和加勒比国家共同体论坛第四届部长级会议开幕式
  • 王毅集体会见加勒比建交国外长及代表
  • 男子发寻母视频被警方批评教育,律师:发寻亲信息是正当行为
  • 学者纠错遭网暴,人民锐评:“饭圈”该走出畸形的怪圈了
  • 来伊份深夜回应“粽子中吃出疑似创可贴”:拿到实物后会查明原因
  • 梅花奖在上海|“我的乱弹我的团”,民营院团首次入围终评