您的位置:首页 > 移动开发 > Objective-C

Introduction to Java Script Object Notation (JSON)

2018-01-24 13:46 507 查看

Jason 入门指南 (Introduction to Java Script Object Notation)

最近需要跨语言进行一些数据交换,基本数据类型数组之类的交换起来太麻烦。

统一将数据编码成Jason字符串进行交换解析。因此研究了一下Json的语法。

这里是使用C++和C#直接传递类的复杂的参数。看了下这本书 (Introduction to Java Script Notation By Lindsay Bassert (O'Reilly). Coppyright 2015 Lindsay Bassett, 978-1-491-92948-3)

另外关于Jason的应用展开会设计大量的JavaScript, NodeJs, JQuery这里不详细讨论。仅研究Jason作为一种数据交换格式。也就是一到五部分。

而6-9部分主要是讲解如何应用Jason的。

一,什么是Json?

Json 是一种数据交换格式

常用于在不同平台直接交换数据,另一种常用的数据交换格式是XML。用于在不同的系统之间交换数据。

Json这种数据交换格式是与语言无关的(不依赖具体语言和系统架构)

虽然Json的全称是JavaScript Object Notation,也就是JavaScript语言的一个子集。但是他作为一种通用的数据交换格式,学习JSON可以完全和JavaScript分离开来。

作者认为可以这样理解JSON(Object Notation 去掉JavaScript的名字)用于表示对象的符号,(object是一种通用的编程概念和具体的语言无关。)

重要的项目和概念

JSON

JavaScript Object Notation

Notation

A System of Characters for representing data such as numbers or elements

Data interchange format

Text used to exchange data between platforms or systems

Portability

Transferring information between platforms in a way that is compatible with both systems

Concepts

JSON is a data interchange format.

JSON is programming language independent(JavaScript is not required to use it)

JSON is based on the object literal notation of JavaScript(emphasis on the word "notation")

JSON represents data in a way that is friendly to universal programming concepts.

二,Json语法

一个使用Jason表示鞋子属性的例子

{
"brand":	"Crocs",
"color":	"pink",
"size":		9,
"hasLaces":	false
}
attributes/properties 表现出名字 值的键值对。

由于JSON主要用于数据交换,重点在于数据。因此不包含函数的定义(definition of function)

名-值 对(Name-Value Pairs)

通常也叫键值对,属性值对,或者字段值对。

"animal": "horse"

JSON语法

定义名称的时候,不要使用特殊符号只是用alpha字符。考虑跨语言兼容问题。

值的部分,字符型采用双引号。

其他类型不需要双引号包括:

数值

布尔值

数组

对象

null

一个大括号包含起来的内容被称为一个对象(object)

{ "animal" : "cat" }

每个名称-值对用逗号分隔

{ "animal": "cat", "color": "orange"}

一个错误的例子

{
title :	"This is my title",
body : "This is the body."
}


名称没有使用双引号。

另一个错误的例子

{
'title' :	'This is my title',
'body' : 'This is the body.'
}
名称和字符串数值使用单引号。

正确的定义如下:

{
"title" : "This is my title",
"body" : "This is the body."
}


推荐一些自动格式化Json的工具:
https://jsonformatter.curiousconcept.com/
这个反应很慢不是太好用。
http://www.jsoneditoronline.org/
这个能根据Json文本生存对象的属性图,并且显示数据的类型格式(采用树形表示),还能手动编辑数据以后生存jason文本。
https://jsonlint.com/
这个反应速度最快,并且能够检查Jason定义的语法。还能自动格式化排版。(推荐)

第四部分介绍一种检查方法叫"conformity vallidation"

Json文件

通常扩展名为.json

Json媒体类型

MIME类型 application/json

重要的概念和项目:

Literal

A value that is written precisely as it is meant to be interpreted

Variable

A value that can be changed and is represented by an identifier, such as x

Maximum rotability(in data interchange)

Transcending the base portablility of the data format by ensuring the data itself will be compatible across systems or platforms

Name-Value Pair

A name-value pair(or key-value pair) is a property or attribute with a name, and a corresponding value

Syntax validation

Validation concerned with the unique data structure

We also discussed these key concepts:

JSON is based on the syntactic representation of the properties of javaScript object literals. This does not include the functions of Javascript object literals.

In the JSON name-value pair, the name is always surrounded by double quotes.

In the JSON name-value pair, the value can be a string, number, boolean, null, object, or array.

The list of name0value pairs in JSON is surrounded by curly brackets.

In JSON, multiple name-value pairs are separated by a comma.

JSON files use the .json extension.

The JSON media type is application/json.

三,Jason数据类型

基本数据类型

数值

整数

浮点数

定点数

字符和字符串

布尔型

枚举类型(数组)

[
"witty",
"charming",
"brave",
"bold"
]


对象类型

{
"brand":	"Crocs",
"color":	"pink",
"size":		9,
"hasLaces":	false
}


一个对象类型是由一个或多个名称-值对组成,每个名称值对可以是基本数据类型也可以是数组也可以是其他对象。

JSON数据类型

对象

字符串

数值

布尔

Null

数组

JSON对象数据类型

{
"person": {
"name": "Lindsay Bassett",
"heightInInches": 66,
"head": {
"hair": {
"color": "light blond",
"length": "short",
"style": "A-line"
},
"eyes": "green"
}
}
}


对象可以嵌套定义。由基本数据类型的名称-值对 或者对象组成

JSON字符串数据类型

{ “animal” : "cat" }

JSON字符串采用Unicode编码可以表示不同语言的字符,字符串类型至于双引号中(不是单引号)

如果在引号中需要使用引号。需要使用转意字符\

{
"promo": "Say \"Bob's the best!\" at checkout for free 8oz bag of kibble."
}


{
"location" : "C:\\Program Files"
}


以下转义字符

\/ (反斜杠)

\b (backspace)

\f (form feed)

\t (tab)

\n (换行)

\r (回车)

\u (后面跟16进制字符 \u263A 表示特殊的unicode符号)

原书上说一下例子有问题(未测试)

{
"story" : "\t Once upon a time, in a far away land \n there lived a princess."
}
要改成这样

{
"story" : "\\t Once upon a time, in a far away land \\n there lived a princess."
}


我总觉得第二个有问题。

JSON 数值类型

{
"widgetInventory" : 289,
"sadSavingsAccount" : 22.59,
"seattleLatitude" : 47.606209,
"seattleLongtitude" : -122.332071,
"earthsMass" : 5.97219e+24
}


可以表示为,整数,小数, 负数,指数。

JSON 布尔类型

小写的true/false

{
"toastWidthBreakfast" : false,
"breadWidthLunch" : true
}


JSON Null数据类型

{
"freckleCount" : 0,
"hairy" : true,
"watchColor" : "blue"
}

{
"freckleCount" : 1,
"hairy" : false,
"watchColor" : null
}


null表示无的意思

JSON数组类型

{
"eggCarton" : [
"egg",
null,
"egg",
"egg",
"egg",
"egg",
"egg",
"egg",
"egg",
"egg",
null,
"egg"
]
}
在json中混合类型是合法的,但在某些强类型语言中这样定义是非法的。

{
"eggCarton" : [
"egg",
null,
"egg",
"egg",
"egg",
"egg",
"egg",
"egg",
"egg",
"egg",
5,
"egg"
]
}
而且不应该这样来定义数组。

字符串数组

{
"students" : [
"Jane Thomas",
"Bob Roberts",
"Robert Bobert",
"Thomas Janerson"
]
}
整型数组

{
"score" : [
93.5,
66.7,
87.6,
92
]
}


布尔数组

{
"answer" : [
true,
false,
false,
true,
false,
true,
true
]
}


对象数组

{
"test" : [
{
"question" : "The sky is blue",
"answer" : true
},
{
"question" : "The earth is flat",
"answer" : false
},
{
"question" : "A cat is a dog."
"answer" : false
}
]
}


二维数组

{
"test" : [
[
true,
false,
false,
false
],
[
true,
true,
true,
false
],
[
true,
false,
true
]
]
}


Key Terms and Concepts

JSON String data type

A String value, like "my string" surrounded in double quotes

JSON boolean data type

A true or false value

JSON number data type

A number value, like 42, that can be a positive or negative integer, decimal, or exponent

JSON null data type

A null value represents an empty value

JSON array data type

An array is a collection or list of values, and the values can have a data type of string, number, boolean, object or array; the values in an array are surrounded by

The boolean data type value of true or false in JSON is always all lowercase characters.

The null data type value in JSON is always all lowercase characters(ie, null, not NULL or Null).

The difference between array and object.

The object is a list or eollection of name-value pairs and an array is a list or collection of values.

An array's values should all have the same data type.

四,Json Scheme

Contracts With Validation Magic

Data Schema 可以认为是一种 “magic” 每次发送数据之前可以作为检查依据,传送的数据是否符合Schema。

JSON Schema可以完成一下任务

1)检查数据类型是否有错误?

是否是数值,字符串等等

2)是否包含了所有需要的数据?

是否有遗漏的数据项

3)是否是我们需要的格式?

检查数据的范围,最大值,最小值等。

Json Schema

定义如下:

{
"$schema" : "http://json-schema.org/draft-04/schema#"
}


定义schema的title

{
"$schema" : "http://json-schema.org/draft-04/schema#",
"title" : "Cat"
}


接着定义json schema文档的properties (有一点类型C++语言中的定义Class)

{
"$schema" : "http://json-schema.org/draft-04/schema#",
"title" : "Cat",
"properties" : {
"name" : {
"type" : "string"
},
"age" : {
"type" : "number"
"description" : "You cat's age in years."
},
"declawed" : {
"type" : "boolean"
}
}
}


以下是基于以上Schema的一个json示例 (有一点想创建了一个Class的实力object并给object赋值)

{
"name" : "Fluffy",
"age" : 2,
"declawed" : false
}


接着增加一些描述哪些属性(properties)是必须的,哪些是可选的。

{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Cat",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "number",
"description": "You cat's age in years."
},
"declawed": {
"type": "boolean"
},
"description": {
"type": "string"
}
},
"required": [
"name",
"age",
"declawed"
]
}


以下是一个基于以上Schema的实例:

{
"name" : "Fluffy",
"age" : 2,
"declawed" : false,
"description" : "Fluffy loves to sleep all day."
}


其中的"description"属性是可选的。

注意,如果在Schema的定义中 "required"数组中没有加入任何属性。那么一个完全为空的json实例是允许的。

{}

最后一个关于Schema的问题是可以定义数据的范围(用以判断数据是否有效)

在Json Schema中可以定义字符的长度,数值的正负以及取值范围。

例如增加了取值约束的json schema

{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Cat",
"properties": {
"name": {
"type": "string",
"minLength": 3,
"maxLength": 20
},
"age": {
"type": "number",
"description": "You cat's age in years.",
"minimum": 0
},
"declawed": {
"type": "boolean"
},
"description": {
"type": "string"
}
},
"required": [
"name",
"age",
"declawed"
]
}


以下json实例不符合定义的schema。 因为名字超出了最大长度。

{
"name" : "Fluffy the greatest cat in the whole wide world",
"age" : 2,
"declawed" : false,
"description" : "Fluffy loves to sleep all day."
}


而以下定义是符合Schema的。

{
"name" : "Fluffy",
"age" : 2,
"declawed" : false,
"description" : "Fluffy loves to sleep all day."
}


以上只是最基本的一个schema的例子,json schema还支持正则表达式 和枚举。 参考json schema可以看到更详细的描述。
http://json-schema.org/ http://json-schema.org/latest/json-schema-validation.html
google上也有不少关于json schema的资料

例如这个可以验证json schema合法性
http://json-schema.org/latest/json-schema-validation.html


一个错误的例子:



另外一个网址
http://jsonschemalint.com/#/version/draft-04/markup/json
同样可以验证Schema和json之间的合法性。



Key Terms and Concepts

JSON Schema

A virtual contract for data interchange

A JSON Validator provides syntax validation, where JSON Schema provides conformity validation.

A JSON Schema can serve as a first line of defense in accepting data or a time(and sanity) saving tool for the party providing the data that ensures their data will conform to what is accepted.

A JSON Schema can answer the following three questions for comformity validation:

Are the data types of the values correct?

We can specify that a value has to be a number, string ,etc.

Does this include the required data?

We can specify what data is required, and what is not.

Are the values in the format that I require?

We can specify ranges, minimum and maximum.

五,Jason 安全隐患

六,JavaScript XmlHttpRequest 和Web API(略)

七,Jason和客户端框架

八,Jason和NoSql

九,Jason和服务器端
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  json
相关文章推荐