JSON.parse() : 字符串-->json对象
var str = '{"name":"huangxiaojian","age":"23"}'//单引号写在{}外,每个属性名都必须用双引号,否则会抛出异常。
JSON.parse(str)
结果:
Object
age: "23"
name: "huangxiaojian"
__proto__: Object
JSON.stringify() : json对象-->字符串
var a = {a:1,b:2}
JSON.stringify(a)
结果:
"{"a":1,"b":2}"
例如:
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<title></title>
</head>
<body>
</body>
<script src="jquery.js"></script>
<script type="text/javascript">
console.log("字符串"+"--->"+"json对象");
var str1 = '{"name":"huangxiaojian","age":"23"}';//单引号写在{}外,每个属性名都必须用双引号,否则会抛出异常。
obj1=JSON.parse(str1);
console.log(str1+"--->");
console.log(obj1);
console.log("json对象"+"--->"+"字符串");
var obj2 = {a:1,b:2};
str2 = JSON.stringify(obj2);
console.log(obj2);
console.log('--->'+str2);
console.log("数组"+"--->"+"字符串");
var array = ["1","2"];
str3 = JSON.stringify(array);
console.log(array);
console.log('--->'+str3);
</script>
</html>