json 语法
JSON which stands for JavaScript Object Notation is a lightweight readable data format that is structurally similar to a JavaScript object much like its name suggests.
代表JavaScript Object Notation的 JSON是一种轻量级的可读数据格式,其结构类似于JavaScript对象,就象其名称所暗示的那样。
It consists of two primary parts- keys and values which together combine to give it the form of the structure that we see. In this article, we'll explore some JSON syntax,
它由两个主要部分-键和值组成,它们结合在一起形成了我们看到的结构形式。 在本文中,我们将探讨一些JSON语法 ,
Key: It is a string enclosed in quotes.
关键字 :这是一个用引号引起来的字符串。
Value: It can be a string, number, boolean expression, array, object, etc and is also enclosed in quotes if it's a string.
值 :它可以是字符串,数字,布尔表达式,数组,对象等,如果是字符串,也可以用引号引起来。
The key-value, when come in together separated by a colon, forms a key-value pair that essentially contains the data inside it. Every key-value pair is separated by a comma, much like in a normal JavaScript Object. The object is enclosed within curly braces.
键值以冒号分隔在一起时,会形成一个键值对,该键值对实际上包含其中的数据。 每个键值对都以逗号分隔,就像在普通JavaScript对象中一样。 该对象括在花括号内。
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}
Above is an example of some JSON data. You can see how all the rules are being followed above? The file extension for a file storing key-value JSON pairs is '.json' and it has a mime type of 'application/json'.
上面是一些JSON数据的示例。 您可以看到上面如何遵循所有规则? 存储键值JSON对的文件的文件扩展名是'.json' ,并且具有mime类型'application / json' 。
You may say that JSON has borrowed or taken JavaScript syntax and it is very true but only to a certain extent. Since it's syntax is highly similar to that of a simple Javascript object people tend to confuse JSON as exactly like a JavaScript object. However, to break your bubble let me point out some differences, you can access a JavaScript object directly but not a JSON.
您可能会说JSON已经借用或采用了JavaScript语法,这是非常正确的,但只是在一定程度上。 由于其语法与简单的Javascript对象的语法高度相似,因此人们倾向于像JSON对象一样混淆JSON。 但是,为打破泡沫,让我指出一些区别,您可以直接访问JavaScript对象,但不能访问JSON。
var obj={
name: 'John',
type:'wwe'
}
Obj;
Obj["name"];
Output
输出量
{name: "John", type: "wwe"}
"John"
We can't do something that we did above with JSON. Also under the key-value pairs, a typical JavaScript object can have methods as properties or the value whereas the value corresponding to a key in JSON can never be a function.
我们无法使用JSON做上面的事情。 同样在键值对下,典型JavaScript对象可以将方法用作属性或值,而与JSON中的键对应的值永远不能是函数。
var pokemon={
name: 'Squirtle',
type: 'Water',
HP: 100,
speak: function(){
console.log(this.name+' says hi!');
}
}
undefined
pokemon.speak();
Output
输出量
Squirtle says hi!
Also, some syntax difference is there like in a JSON the key is always enclosed inside quotes unlike in a JavaScript object where the quotes enclose a key only if deemed necessary. We can say the syntax that JSON carries is essentially a subset of the syntax of a JavaScript Object, with some additions to it.
而且,在语法上存在一些差异,例如在JSON中,密钥始终被括在引号内,这与JavaScript对象不同,在JavaScript对象中,引号仅在认为必要时才包含密钥。 我们可以说JSON所承载的语法本质上是JavaScript对象语法的子集,并且有一些补充。
翻译自: https://www.includehelp.com/json/basic-syntax-of-json.aspx
json 语法