JSON,也就是 JavaScript Object Notation,是一種常見的資料表示格式。它使用物件與陣列結構,能清楚地表達鍵值對與有序資料。
這兩種結構可以互相嵌套,因此能表達相當複雜的資料。
var author = {
name: "trigkit4",
age: 21,
sex: "male"
};
var company = {
name: "Apple",
chairman: {
name: "Tim Cook",
age: 54
},
employees: [
{ name: "Jony Ive", age: 47 },
{ name: "Lili", age: 29 }
]
};在 JavaScript 中,可以透過點號或中括號取得物件屬性。例如 author.name 或 company.employees[0].name。
JavaScript 有 Undefined、Null、Boolean、Number、String 等基本型別,也有 Object 這種複雜型別。使用 typeof 可以初步判斷型別。
typeof "hello"; // string
typeof 21; // number
typeof true; // boolean
typeof undefined; // undefined
typeof null; // object
typeof {}; // object其中 null 的 typeof 結果是 object,這是 JavaScript 的歷史特性。理解這些型別與 JSON 結構,有助於處理表單資料、Ajax 回應與 API 交換格式。