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 語法