JSON数据有以下形式
对象: 对象之内还能嵌套对象,名称只能用String来表示
形式: {名称/值}
例子: {"数学成绩":95,"语文成绩":89}
数组:值的有序集合
形式: [值],值之间用逗号分隔
例子:[1,4,5]
值:可以是字符串,数值,布尔值,对象,数组
字符串:由双引号包围的任意数量Unicode字符的集合
例子:"hello world"
将 JSON 数据赋值给变量:
var people = { "programmers": [ { "firstName": "Brett", "lastName":"McLaughlin", "email": "[email protected]" }, { "firstName": "Jason", "lastName":"Hunter", "email": "[email protected]" }, { "firstName": "Elliotte", "lastName":"Harold", "email": "[email protected]" } ], "authors": [ { "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" }, { "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" }, { "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" } ], "musicians": [ { "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" }, { "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" } ] } |
访问数据的方式:
people.programmers[0].lastName; |
修改数据的方式:
people.musicians[1].lastName = "Rachmaninov"; |
将JSON数据转换为字符串
String newJSONtext = people.toJSONString();
可以任何 JavaScript 对象转换为 JSON 文本
JSON的结构只有2种形式:对象,数组
JSON的值可以是: 数字,对象,数组,字符串,布尔值