简体   繁体   中英

How to store JSON data into JavaScript table format?

I am developing a web application and I use jQuery 1.5 and JavaScript for the main functionality of the app. I connect from my app to a RESTful interface where I GET information for a person. I use this function to retrieve the information from the json page:

var jqxhr = $.getJSON("example.json", function() { // store the data in a table }

My data in json format are like but I will get as a result more than one persons having the format of:

[{"person":{"time":"2010-02-18T17:59:44","id":1,"name": "John","age":60, "updated_at":"010-02-18T17:59:44"}}]

How can I store only the id, the name and the age of the person in a JavaScript table (to be more precise an array) and ignore the rest of the information?

Here is the specific JavaScript / jQuery you need, based on the MAP function.

var originalData = [
    { "person": { "time": "2010-02-18T17:59:34", "id": 1, "name": "John", "age": 60, "updated_at": "010-02-18T17:59:41"} },
    { "person": { "time": "2010-02-18T17:59:44", "id": 2, "name": "Bob", "age": 50, "updated_at": "010-02-18T17:59:42"} },
    { "person": { "time": "2010-02-18T17:59:54", "id": 3, "name": "Sam", "age": 40, "updated_at": "010-02-18T17:59:43"} }
];

var data = $.map(originalData, function (ele) {
    return { id: ele.person.id, name: ele.person.name, age: ele.person.age };
});

Here is a full example that will convert and display the results in HTML.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="Styles/Site.css" rel="stylesheet" type="text/css" />
    <script src="Scripts/jquery-1.6.1.js" type="text/javascript"></script>
    <script type="text/javascript">

        function CreateTableView(objArray, theme, enableHeader) {
            // set optional theme parameter
            if (theme === undefined) {
                theme = 'mediumTable'; //default theme
            }

            if (enableHeader === undefined) {
                enableHeader = true; //default enable headers
            }

            // If the returned data is an object do nothing, else try to parse
            var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;

            var str = '<table class="' + theme + '">';

            // table head
            if (enableHeader) {
                str += '<thead><tr>';
                for (var index in array[0]) {
                    str += '<th scope="col">' + index + '</th>';
                }
                str += '</tr></thead>';
            }

            // table body
            str += '<tbody>';
            for (var i = 0; i < array.length; i++) {
                str += (i % 2 == 0) ? '<tr class="alt">' : '<tr>';
                for (var index in array[i]) {
                    str += '<td>' + array[i][index] + '</td>';
                }
                str += '</tr>';
            }
            str += '</tbody>'
            str += '</table>';
            return str;
        }


        $(document).ready(function () {
            var originalData = [
            { "person": { "time": "2010-02-18T17:59:34", "id": 1, "name": "John", "age": 60, "updated_at": "010-02-18T17:59:41"} },
            { "person": { "time": "2010-02-18T17:59:44", "id": 2, "name": "Bob", "age": 50, "updated_at": "010-02-18T17:59:42"} },
            { "person": { "time": "2010-02-18T17:59:54", "id": 3, "name": "Sam", "age": 40, "updated_at": "010-02-18T17:59:43"} }
        ];

            var data = $.map(originalData, function (ele) {
                return { id: ele.person.id, name: ele.person.name, age: ele.person.age };
            });

            $('#results').append(CreateTableView(data, 'lightPro', true));
        });
    </script>
</head>
<body>
    <div id="results" style="width: 500px; margin: 20px auto;">
</div>

You can use jQuery's map function:

var data = $.map(originalData, function(person) {
    return { id: person.id, name: person.name, age: person.age };
});

map basically converts each item in an Array , producing a new Array with the modified objects.

    $.getJSON("example.json", function(data) { 
var name = data.person.name;
var id = data.person.id;
var age = data.person.age;
}

what do exactly mean by a javascript table u can store in a html table by

var $table = $("<table>
<tr><td>name</td><td>"+name+"</td></tr>
<tr><td>id</td><td>"+id+"</td></tr>
<tr><td>age</td><td>"+age+"</td></tr>
</table>");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM