1.无加密数据
{"name":"xiegaorui","age":"23"}
var json = eval("(" + data.r + ")");
var json=JSON.parse(str);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>改进js</title>
<script type="text/javascript">
window.onload=function(){
var str='{"nam":"小谢","age":"24"}';
// var json=eval("("+str+")");
var json=JSON.parse(str);
var xiugai=document.getElementById("name").innerHTML=json.nam;
}
</script>
</head>
<body>
<span id="name">谢高瑞</span>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>改进js</title>
<script type="text/javascript">
window.onload=function(){
var people = '{"firstname":"Jesper","surname":"Aaberg","phone":["555-0100","555-0120"]}';
debugger;
var json=JSON.parse(people); //解析后的json = Object {firstname: "Jesper", surname: "Aaberg", phone: Array[2]},
document.getElementById("name").innerHTML=json.phone[1];//或者json["phone"][0]
};
</script>
</head>
<body>
<span id="name">谢高瑞</span>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>改进js</title>
<script type="text/javascript">
window.onload=function(){
var people ='{"name":"王五","age":"18","work":{"name":"programer","salary":"3k"}}';
//debugger;
var json=JSON.parse(people);//解析后的json json = Object {name: "王五", age: "18", work: Object},
document.getElementById("name").innerHTML=json.work.name;
};
</script>
</head>
<body>
<span id="name">谢高瑞</span>
</body>
</html>
2.json数组
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>改进js</title>
<script type="text/javascript">
window.onload=function(){
var people = {
"programmers": [{
"firstName": "Brett",
"lastName": "McLaughlin",
"email": "aaaa"
}, {
"firstName": "Jason",
"lastName": "Hunter",
"email": "bbbb"
}],
"authors": [{
"firstName": "Isaac",
"lastName": "Asimov",
"genre": "sciencefiction"
}, {
"firstName": "Tad",
"lastName": "Williams",
"genre": "fantasy"
}],
"musicians": [{
"firstName": "Eric",
"lastName": "Clapton",
"instrument": "guitar"
}, {
"firstName": "Sergei",
"lastName": "Rachmaninoff",
"instrument": "piano"
}]
};
debugger;
var json=eval(people);//解析后的json = Object {programmers: Array[2], authors: Array[2], musicians: Array[2]}
document.getElementById("name").innerHTML=json.authors[0].genre;
};
</script>
</head>
<body>
<span id="name">谢高瑞</span>
</body>
</html>
2.有加密数据解密
var json = eval("(" + decodeURIComponent(data.r )+ ")");