简体   繁体   English

json数据未返回值

[英]json data not returning values

I'm currently trying to "foreach" some json data, but console is not returning anything, while it should return values (integers) of progress bars. 我目前正在尝试“散布”一些json数据,但控制台未返回任何内容,而应返回进度条的值(整数)。

My current PHP file that should return the json data 我当前的PHP文件应返回json数据

$jsonData = [
    'progress' => [
        'health_pc' => $my->hp_percent,
        'energy_pc' => $my->energy_percent,
        'awake_pc'  => $my->awake_percent,
        'nerve_pc'  => $my->nerve_percent,
        'exp_pc'    => $my->exp_percent,
    ]
];

echo json_encode($jsonData);

How I handle it in javascript: 我如何在javascript中处理它:

// Progress Bars
            $.each(jsonData.progress, function() {
                $.each(this, function(k, v) {
                    console.log(k + '-' + v);
                });
            });    

Note that the console.log() doesn't log anything. 请注意, console.log()不会记录任何内容。

What i'm trying to achieve is to adjust the progress bars when the %'s change, normally I would do: 我要达到的目的是在%的更改时调整进度条,通常我会这样做:

$('#health_pc').css('width', jsonData.progress.health_pc + '%');
$('#energy_pc').css('width', jsonData.progress.energy_pc + '%');
$('#awake_pc').css('width', jsonData.progress.awake_pc + '%');
$('#nerve_pc').css('width', jsonData.progress.nerve_pc + '%');
$('#exp_pc').css('width', jsonData.progress.exp_pc + '%');

but I want to foreach this rather than writing this all out. 但是我想讲这个而不是全部写出来。

Any help would be greatly appreciated. 任何帮助将不胜感激。

Edit 1 Output of json_encode() 编辑 json_encode() 1个输出

{"progress":{"health_pc":100,"energy_pc":100,"awake_pc":100,"nerve_pc":100,"exp_pc":3}}

You must change your jQuery function to the following, you don't need two each methods. 您必须将jQuery函数更改为以下内容, each方法不需要两个。

 const jsonData = {"progress":{"health_pc":100,"energy_pc":100,"awake_pc":100,"nerve_pc":100,"exp_pc":3}}; console.log(jsonData); $.each(jsonData.progress, function(k, v) { console.log(k + '-' + v); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

As long as the php variables are no arrays themselves one level of for each should be enough: 只要php变量本身没有数组,每个变量的一个级别就足够了:

$.each(jsonData.progress, function(k, v) {
     $('#'+k).css('width', v + '%');
})

You can use a vanilla javascript for loop. 您可以使用普通javascript for循环。

 let $jsonData = { 'progress': { 'health_pc': 15, 'energy_pc': 33, 'awake_pc': 21, 'nerve_pc': 87, 'exp_pc': 9, } }; let myFunction = function() { for (var key in $jsonData.progress) { if ($jsonData.progress.hasOwnProperty(key)) { $('#' + key).css('width', $jsonData.progress[key] + '%'); $('#' + key).children('span').html($jsonData.progress[key] + '%'); } } }; myFunction(); 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="progress"> <div class="progress-bar" role="progressbar" aria-valuenow="70" aria-valuemin="0" aria-valuemax="100" id='health_pc' style="width:70%"> <span></span> </div> </div> <div class="progress"> <div class="progress-bar" role="progressbar" aria-valuenow="70" aria-valuemin="0" aria-valuemax="100" id='energy_pc' style="width:70%"> <span></span> </div> </div> <div class="progress"> <div class="progress-bar" role="progressbar" aria-valuenow="70" aria-valuemin="0" aria-valuemax="100" id='awake_pc' style="width:70%"> <span></span> </div> </div> <div class="progress"> <div class="progress-bar" role="progressbar" aria-valuenow="70" aria-valuemin="0" aria-valuemax="100" id='nerve_pc' style="width:70%"> <span></span> </div> </div> <div class="progress"> <div class="progress-bar" role="progressbar" aria-valuenow="70" aria-valuemin="0" aria-valuemax="100" id='exp_pc' style="width:70%"> <span></span> </div> </div> 

Hope it helps! 希望能帮助到你!

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

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