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之间的一个整数
$表示字符串结尾