简体   繁体   English

如何使用javascript显示JSON数组数据?

[英]How to show JSON array data by using javascript?

I have a ajax returning json array data like this, 我有一个ajax这样返回json数组数据,

[{
    "UserName": "John",
    "Total": "45",
    "Correct(%)": "71.1111",
    "Incorrect(%)": "28.8889"
}, {
    "UserName": "KKK",
    "Total": "42",
    "Correct(%)": "47.6190",
    "Incorrect(%)": "52.3810"
}, {
    "UserName": "AAA",
    "Total": "54",
    "Correct(%)": "81.4815",
    "Incorrect(%)": "18.5185"
}, {
    "UserName": "BBB",
    "Total": "39",
    "Correct(%)": "58.9744",
    "Incorrect(%)": "41.0256"
}]

I want to show that data by using javascript like this, 我想使用像这样的javascript显示数据,

UserName: John Total: 45 Correct(%): 71.1111 用户名:John总计:45正确(%):71.1111
Incorrect(%): 28.8889 不正确(%):28.8889

UserName: KKK Total: 42 Correct(%): 47.6190 用户名:KKK总计:42正确(%):47.6190
Incorrect(%): 52.3810 不正确(%):52.3810

UserName: AAA Total: 54 Correct(%): 81.4815 用户名:AAA总计:54正确(%):81.4815
Incorrect(%): 18.5185 不正确(%):18.5185

UserName: BBB Total: 39 Correct(%): 58.9744 用户名:BBB总计:39正确(%):58.9744
Incorrect(%): 41.0256 不正确(%):41.0256

So, I try like this, 所以,我这样尝试

$.ajax({
                url: 'some.php',
                type: 'post',
                data: {dept:d},
                dataType: 'json',
                success: function(data) {
                    console.log("success");
                    var temp = "";
                    if(data && data!="") {
                        for(var i=0; i<data.length; i++) {
                            $.each(data,function(k,v){
                                $.each(v,function(k,s){
                                    temp +=k+': <b>'+s+'</b> &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp';
                                });
                                temp +="<br/><br/>";
                                console.log(temp);
                            });
                        }

                        document.getElementById('user').innerHTML= temp;
                    }
});

But, I got five line for each user. 但是,我为每个用户分配了五行。 I was wrong while looping. 循环时我错了。 So, how can I do this? 那么,我该怎么做呢?

Try removing the for loop. 尝试删除for循环。

 //for(var i=0; i<data.length; i++) {
    $.each(data,function(k,v){
        $.each(v,function(k,s){
           temp +=k+': <b>'+s+'</b> &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp';
        });
        temp +="<br/><br/>";
        console.log(temp);
    });
 //}

Use this code: 使用此代码:

$.ajax({
    url: 'some.php',
    type: 'post',
    data: {dept:d},
    dataType: 'json',
    success: function(data) {
        console.log("success");
        var temp = '';
        if(data && data != "") {
            var temp = '<ul>';
            for(var i = 0; i < data.length; i++) {
                temp += '<li>UserName: ' + data[i]['UserName'] + '&nbsp;Total: ' + data[i]['Total'] + '&nbsp;Correct(%): ' + data[i]['Correct(%)'] + '&nbsp;Incorrect(%): ' + data[i]['Incorrect(%)'] + '</li>';
            }
            temp += '</ul>';

            document.getElementById('user').innerHTML = temp;
        }
    }
});

您只需要使用1 $ .each循环,否则就循环遍历所有数据时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用AJAX显示JSON数据内部数组的数据? - How to show data of inner Array of JSON data using AJAX? 如何使用javascript jquery在html页面中显示json(array)响应 - how to show json (array)response in html page using javascript jquery 如何转换 json 并显示 html 中的所有数据? 使用 Javascript 和 Map - How to convert json and show all data in html ? using Javascript and Map 如何使用Javascript解析json并在下拉按钮中显示数据? - How to parse json and show data in a dropdown button using Javascript? 如何在JavaScript中将数组与json数据进行比较,并在另一个数组中显示匹配的json结果 - how to compare array with json data in javascript and show the matched json result in another array 如何使用javascript访问数组的JSON编码数据 - How to access JSON encoded data of an array using javascript 如何使用JavaScript获取json数据中的数组名称 - How to get the name of the array in json data using JavaScript 如何使用控制器 laravel 制作数据数组 json javascript - How to make a data array json javascript using controller laravel 如何使用JavaScript访问JSON数据中嵌套数组中的特定元素? - How to access a specific element in nested array in JSON data using JavaScript? 如何使用JavaScript查找JSON数组中新增的数据 - How to search newly added data in JSON array using JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM