<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>jq的ajax查询修改json</title>
<script src="jquery-1.12.3.js"></script>
</head>
<body>
<h3>只输入ID号为查询,补全为添加新人信息</h3>
ID号:<input type="text"><br>
姓名:<input type="text"><br>
邮箱:<input type="text"><br>
根据ID查询的内容: <p type="text"></p><br>
新添加的个人信息 <p type="text"></p><br>
<button>查询</button><br>
<button>添加</button>
</body>
<script>
$('document').ready(function(){
// 点击获取json文件的内容
$(".chaxun").click(function(){
$.ajax({
url: 'jsonpage.json',
type: 'GET',
dataType: 'json',
})
.done(function(res) {
var objj='res';
/*
var obj=JSON.parse(objj);
使用这个JSON.pares()的时候报以下的错:
“SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data”并指向上面那行。
*/
// 下列发现只是展示了第一个数组:“root”,而没有第二个数组;
var obj=eval("("+objj+")");
console.log(obj);
console.log(obj[1].email);
})
});
/*
问:
1.如何获取json文件的指定对象或者是指定数组。
2.为什么JSON.pares()方法报错。
3.为什么获取的是整个json文件内容,但是只展示第一个对应的数组。
*/
})
</script>
</html>
=======================================下列是jsonpage.json的所有内容========================================
{
"root":
[
{"uid":"0","name":"bill","email":"124548645@163.com"},
{"uid":"1","name":"tomz","email":"gooodaja1@qq.com"}
],
"hood":
[
{"uid":"0","name":"zip","email":"1freeque645@189.com"},
{"uid":"1","name":"hams","email":"486da451@gmail.com"}
],
}