更新json数组值并使用jquery将更新的json保存在外部文件中?

时间:2018-12-07 09:07:28

标签: php jquery arrays json

我通过使用外部json对象创建一个表。它可以正常工作,但是我想通过使用数组格式来更新给定的json数据,并且更新后的json值应存储在另一个外部文件中。如何使用jquery更新和保存数据。

<table class="table table-hover">
        <thead>
            <tr>                
                <th>Team</th>
                <th>Name</th>
                <th>Score</th>
            </tr>
        </thead>
        <tbody id="vehicleList">

        </tbody>
    </table>
    $(document).ready(function() {
        // Fetch the initial table
        refreshTable();

        // Fetch every 5 seconds
        setInterval(refreshTable, 5000);
    });

    function refreshTable(){
        $.getJSON('json/data.json', function(data) {
            var vehicleListData = [];

            /* sort the table datas */
            data.sort(function(a,b) { 
                return parseFloat(a.team_id) - parseFloat(b.team_id) 
            } );

            $.each(data, function(key, value) {
                vehicleListData += '<tr id="rowVehicleStatus" class="">';
                vehicleListData += '<td>'+value.team_id+'</td>';
                vehicleListData += '<td>'+value.User_Name+'</td>';
                vehicleListData += '<td>'+value.score+'</td>';
                vehicleListData += '</tr>';     
            });

            // We use .html instead of .append here, to make sure we don't add the same
            // entries when the interval is ran for the n-th time.
            $('#vehicleList').html(vehicleListData);
        });
    } 

data.json:

 [
        {
            "User_Name":"John Doe",
            "score":"10",
            "team_id":"101"
        },
        {
            "User_Name":"Jane Smith",
            "score":"15",
            "team_id":"102"
        },
        {
            "User_Name":"Chuck Berry",
            "score":"12",
            "team_id":"103"
        },
        {
            "User_Name":"Chuck",
            "score":"13",
            "team_id":"105"
        },
        {
            "User_Name":"Berry",
            "score":"18",
            "team_id":"104"
        }
    ] 

我想通过比较ID将用户名berry更新为berry123。而且,我想在外部文件中显示新的更新的json数组值。

0 个答案:

没有答案