{
"employees": [
{ "firstName":"Bill" , "lastName":"Gates" },
{ "firstName":"George" , "lastName":"Bush" },
{ "firstName":"Thomas" , "lastName":"Carter" }
]
}
这是json页面
html页面怎么写呢?求大神??
就是打印出来firstName
<!DOCTYPE html> <html> <head> <meta charset='UTF-8'> <title>json</title> <script type="text/javascript" src='jquery-2.2.0.min.js'></script> </head> <body> </body> <script type="text/javascript"> $(function(){ $.ajax({ type:'GET', url:'data.json', data:{}, dataType:'json', success:function(data){ for(var i=0;i<data.employees.length;i++){ console.log(data.employees[i].firstName); } } }) }) </script> </html>
什么意思?js天生支持json的啊。。。
用jquery从服务器拿到这数据你是知道的吧?然后JSON.parse或者eval一下,然后就是对象了,想怎么取怎么取。。。
不是我的本意是想看怎么用ajax呢 并不是说js解决,就是从json页面拿取数据
就是不知道呢 ajax生疏了 不会用了
test.employees[0].firstName
test.employees[1].firstName
test.employees[2].firstName
<script>
var test =
{
"employees": [
{ "firstName": "Bill", "lastName": "Gates" },
{ "firstName": "George", "lastName": "Bush" },
{ "firstName": "Thomas", "lastName": "Carter" }
]
}
alert(test.employees[0].firstName);
alert(test.employees[1].firstName);
alert(test.employees[2].firstName);
</script>
@小小高: 不是我的本意是想看怎么用ajax呢 并不是说js解决,就是从json页面拿取数据
@小码农雯:
$.ajax({
type:"get",
url:"你的路径",
success:function(test){
alert(test.employees[0].firstName);
},
error:function(a,b,c){alert(a);alert(b);alert(c);}
});