从URL使用AJAX显示JSON数据 [英] Display JSON data from url using ajax
问题描述
我们需要显示我们的JSON文件的所有名字到使用javascript一个div。我们尝试了很多东西,但我们没能成功。
JSON数据: http://www.smartbustracking.be/json/data.json一>
这是我们尝试:
<按钮>获取数据和LT; /按钮>
< DIV ID =resultJson>< / DIV>
<脚本类型=文/ JavaScript的LANGUAGE =JavaScript的>
$(按钮)。点击(函数(){
$ .getJSON(http://www.smartbustracking.be/json/data.json功能(结果){
为(变种X = 0 X - 其中; result.length; X ++){
$每个(结果,功能(我,场){
$(#resultJson)追加(场[X]。名称)。
}
}
});
});
如果任何人都可以帮助我们完成这一ploblem,那将是巨大的。
在此先感谢!
不使用额外的循环中循环提取的名字。
您的数据就像
[0] = {名NAME1}
[1] = {名字:NAME2}
如果里面有一个数组[0],你想,那么你需要另外一个循环内循环来提取值就像你想拉 bushaltes $ C的值来提取该值$ C>。
试试这样
$(按钮)。点击(函数(){
$ .getJSON(http://www.smartbustracking.be/json/data.json功能(结果){
执行console.log(结果);
为(变种X = 0 X - 其中; result.length; X ++){
$(#resultJson)追加(结果[X]。名称)。
}
});
});
&LT;脚本SRC =https://ajax.googleapis.com/ajax /libs/jquery/2.1.1/jquery.min.js"></script>
&LT;按钮&GT;获取数据和LT; /按钮&GT;
&LT; DIV ID =resultJson&GT;&LT; / DIV&GT;
We want to display all the names from our json file into a div using javascript. We tried a lot of things, but we didn't manage to succeed.
json data : http://www.smartbustracking.be/json/data.json
This is what we tried :
<button>get data</button>
<div id="resultJson"></div>
<script type="text/javascript" language="javascript">
$("button").click(function(){
$.getJSON("http://www.smartbustracking.be/json/data.json", function(result){
for(var x = 0; x < result.length; x++){
$.each(result, function(i, field) {
$("#resultJson").append(field[x].name);
}
}
});
});
If anyone can help us with this ploblem, that would be great
Thanks in advance !
don't use extra loop inside for loop to extract name.
Your data is like
[0] = {name:"name1"}
[1] = { name: "name2"}
If there is a array inside [0] and you want to extract value from that then you need another loop inside for loop to extract that value like you wanna to pull value of bushaltes
.
Try like this
$("button").click(function() {
$.getJSON("http://www.smartbustracking.be/json/data.json", function(result) {
console.log(result);
for (var x = 0; x < result.length; x++) {
$("#resultJson").append(result[x].name);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>get data</button>
<div id="resultJson"></div>
这篇关于从URL使用AJAX显示JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!