且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从URL使用AJAX显示JSON数据

更新时间:2023-02-23 21:32:05

不使用额外的循环中循环提取的名字。

您的数据就像

  [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 =htt​​ps://ajax.googleapis.com/ajax /libs/jquery/2.1.1/jquery.min.js"></script>
&LT;按钮&GT;获取数据和LT; /按钮&GT;
&LT; D​​IV 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>