Php 使用jquery和HTML解析json数据
我正在尝试使用Jquery解析来自远程URL的json数据。我的数据格式如下:Php 使用jquery和HTML解析json数据,php,jquery,json,parsing,Php,Jquery,Json,Parsing,我正在尝试使用Jquery解析来自远程URL的json数据。我的数据格式如下: [ { "UserId": "5", "Name": "Syed", "Lat": "23.193458922305805", "Long": "77.43331186580654", "EmailId": "syedrizwan@ats.in", "LocationUpdatedAt": "" }, { "UserId": "98", "Name":
[
{
"UserId": "5",
"Name": "Syed",
"Lat": "23.193458922305805",
"Long": "77.43331186580654",
"EmailId": "syedrizwan@ats.in",
"LocationUpdatedAt": ""
},
{
"UserId": "98",
"Name": "Michael Catholic",
"Lat": "23.221318",
"Long": "77.42625",
"EmailId": "michaelcatholic@gmail.com",
"LocationUpdatedAt": ""
}
]
我已经检查了json lint中的数据,它说这是正确的数据格式。当我尝试在HTML页面上运行它时,它返回一个空白页面。我的HTML代码如下所示:
<html>
<head>
<title>Jquery Json</title>
<script type="text/javascript" src="jquery.js"></script>
<script>
$(document).ready(function(){
$.getJSON('http://localhost/fbJson/json.php', function(results){
document.write(results.Name);
});
});
</script>
</head>
<body>
</body>
</html>
Jquery Json
$(文档).ready(函数(){
$.getJSON('http://localhost/fbJson/json.php,函数(结果){
文件。书写(结果。名称);
});
});
我试图从json字符串中检索名称。有一个对象数组,您可以使用索引直接访问它们
$.getJSON('http://localhost/fbJson/json.php', function(results){
document.write(results[0].Name);
});
如果希望在数组上迭代,可以使用$。每个并将结果传递给它
$.each(results, function(key, val) {
document.write(val.Name);
});
<代码>结果是一个项目数组,因此您必须考虑您想要哪个项目。
通常情况下,您将以类似于以下方式的方式循环遍历阵列:
$.getJSON('http://localhost/fbJson/json.php', function(results){
for(var i = 0; i < results.length; i++) {
console.log(results[i].Name);
}
});
$.getJSON('http://localhost/fbJson/json.php,函数(结果){
对于(var i=0;i
您的json格式是正确的。
使用以下代码访问第一行数组中的名称:
document.write(results[0].Name);