如何通过JQUERY将JSON转换为HTML?

时间:2019-04-28 16:05:03

标签: jquery html json undefined

我正在尝试将一些JSON数据解析为网页。我不断收到应显示JQUERY变量的“未定义”错误。我已经浏览过以前的解决方案;不幸的是,由于我是新手,所以它们在我头上。知道我在做什么错吗?

这是我的JSON内容:

[{"v_avg2":"-1.93","v_max2":"9.40","v_min2":"-12.30","v_std2":"7.42","s_avg2":"-0.18","s_max2":"7.87","s_min2":"-9.18","s_std2":"5.47","alpha_avg2":"-1.75","alpha_max2":"1.53","alpha_min2":"-4.76","VCAGR":"-26.542465909469826","SCAGR":"-4.004316028237165"}]

这是我的HTML:

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src='https://code.jquery.com/jquery-3.2.1.min.js'></script>
    </head>
<body>

<h2>Testing HTML/JSON</h2>

<div>
    <span id="nums"></span>
</div>

<script>
fetch('./php/beta0472019.php')
.then(function (response){
    return response.json();
})
.then(function (json){
    console.log(json);
    var vavg = json.v_avg2;
    $('#nums').html('VIOG Mean: <span style="float:right;">' +vavg+ '</span><br>');         
});
</script>


</body>
</html>

对于初学者,任何帮助将不胜感激。

谢谢!

1 个答案:

答案 0 :(得分:0)

json[0].v_avg2,因为它是一个数组