123456789101112131415161718192021222324252627282930313233343536 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <script>
- // 使用一串字符串来描述对象
- var personStr = '{"name":"张三","age":20,"interests":["篮球","足球","乒乓球"]}'
- console.log(personStr)
- console.log(typeof personStr)
- // 把对象和字符串之间互相转换
- var person = JSON.parse(personStr)
- console.log(person)
- var person1 = {
- // 格式: 属性名:属性值, 属性名:属性值
- "name": "张三",
- "age": 20,
- "interests": ["篮球", "足球", "乒乓球"],
- "info": function(){
- console.log(this.name + "的年龄" + this.age + ",喜欢的运动有:")
- // 对数组元素的遍历
- for (var i = 0; i < this.interests.length; i++) {
- console.log(this.interests[i])
- }
- }
- }
- var personStr1 = JSON.stringify(person1)
- console.log(personStr1)
- </script>
- </head>
- <body>
- Hi Json
- </body>
- </html>
|