1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <title>Document</title> 7 8 <script> 9 /* 10 JSON格式的语法 11 var personStr = '{"属性名":"属性值", "属性名":属性值, "属性名":{}, "属性名":["","",""], "属性名":[{},{},{}], ...:...}' 12 属性名必须用"",属性值如果是字符串,必须用"",数字可以不处理 13 */ 14 15 //一个JSON格式的字符串 16 var personStr = '{"name":"张三", "age":20, "dog":{"dname":"小花", "dage":2}, "loveSingers":["张小明","王小东","李小红"], "friends":[{"fname":"李四"},{"fname":"王五"},{"fname":"赵六"}]}' 17 console.log(personStr) 18 19 //通过JSON.parse()可以将一个JSON串转换为一个对象 20 var person = JSON.parse(personStr) 21 console.log(person) 22 console.log(person.name) 23 console.log(person.dog.dname) 24 console.log(person.loveSingers[0]) 25 console.log(person.friends[0].fname) 26 27 //通过 JSON.stringify() 将一个对象转换为JSON串 28 var personStr1 = JSON.stringify(person) 29 console.log(personStr1) 30 31 </script> 32 33 </head> 34 <body> 35 36 </body> 37 </html>
标签:console,log,08JSON,person,JSON,fname,格式,属性 From: https://www.cnblogs.com/IrVolcano/p/18074195