我想将x(time)的值作为参数传递给函数,以便我可以从JSON中读取相关数据。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>JSON Sample</title>
</head>
<body>
<div id="placeholder"></div>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script>
var x="time";
$.getJSON('data.json', function(data) {
var output="<ul>";
for (var i in data.data)
{
output+="<li>" + data.data[i].x + "</li>";
&lt; - 代替data.data [i] .x如果使用data.data [i] .svctime然后我得到我的预期输出,但我想从变量传递值! - &gt;
}
output+="</ul>";
document.getElementById("placeholder").innerHTML=output;
});
</script>
</body>
</html>
Json File(data.Json)
{
"data": [
{
"time": "0",
"count": 19079118,
"percent": "68.27%"
},
{
"time": "1",
"count": 7542910,
"percent": "26.99%"
},
{
"time": "2",
"count": 877308,
"percent": "3.14%"
},
{
"time": "3",
"count": 234513,
"percent": "0.84%"
},
{
"time": "4",
"count": 84972,
"percent": "0.30%"
}
]
}
我的预期输出(使用data.data [i] .time时):
0
1
2
3
4
我看到的输出是(使用data.data [i] .x时):
undefined
undefined
undefined
undefined
undefined
请建议我如何传递字符串以通过功能检索
答案 0 :(得分:0)
for (var i in data.data)
{
output+="<li>" + data.data[i][x] + "</li>";
}