基本介紹-
JSON與XML-
- JSON
{"employees":[ {"firstName":"John", "lastName":"Doe"}, {"firstName":"Anna", "lastName":"Smith"}, {"firstName":"Peter", "lastName":"Jones"} ]}
- XML
簡單的以上面兩個範例來解釋,兩者皆表達一樣的意思,但可以發現XML需要耗費更多標籤也更繁瑣!
JSON 語法規則-
- 資料皆以key : value成對記錄
- 資料與資料間以 , (comma) 分隔
- { }用來記錄物件
- [ ]用來記錄陣列
- value可為number,string,boolean,array,object
Javascript運用JSON小範例-
var employees = [ {"firstName":"John", "lastName":"Doe"}, {"firstName":"Anna", "lastName":"Smith"}, {"firstName":"Peter", "lastName": "Jones"} ];
意思為有一個employees陣列裡有三個物件
employees[0].firstName + " " + employees[0].lastName;
結果為John Doe
employees[0].firstName = "Gilbert";
上述語法則是修改John為Gilbert
沒有留言:
張貼留言