<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
<!-- 模块 -->
<script>
// JSON字符串 使用双引号将JSON对象进行包裹
const JSON_Str= '{ "type":"SUV", "color":"红色", "price":200000, "isBuy":false, "liwu":["遮光膜","10次保养","20元兰博基尼优惠券"], "player":{ "carName":"摇一摇电动车", "carPrice":200, "carCreateTime":"2023-06-15" }, "buyUser":null }'
const JSON_Arr = '["老虎","狮子","熊猫",true,20000,{"dogName":"小白"},null]';
//将JSON字符串转换为js对象/数组
/*
方法:JSON.parse(JSON字符串)
返回值:转换后的JS对象/数组
转换后就可以按照JS的方式读取值
*/
const JS_OBJECT = JSON.parse(JSON_Str)
console.log(JS_OBJECT.type);//通过对象.属性获取值
const JS_ARRAY = JSON.parse(JSON_Arr)
console.log(JS_ARRAY[0]);
//将JS对象转换为JSON字符串
/*
方法:JSON.stringify(js对象/数组)
返回值:转换后的JSON字符串
转换后js对象/数组 就变成了字符串
*/
const DOG ={ dname:"小黄", dtype:"拉布拉多" }
localStorage.setItem('dog',JSON.stringify(DOG));//以JSON字符串的形式存储
console.log(localStorage.getItem('dog'));//返回结果为JSON字符串
console.log(JSON.parse(localStorage.getItem('dog')))
</script>
</html>