我有一个以下类型的数组,实际上非常长:
[["Vishnu", 25],["Joginder", 22],["Amar", 27],["Rohan", 24],["Karan", 23]]
我首先将它们转换为以下类型的JSON数组:
[
{
"Player": "Vishnu",
"Age": 25
},
{
"Player": "Joginder",
"Age": 22
},
{
"Player": "Amar",
"Age": 27
},
{
"Player": "Rohan",
"Age": 24
},
{
"Player": "Karan",
"Age": 23
}
]
我想在异步调用d3.json的帮助下创建表,因为数组在另一个url上。我做了以下Javascript代码
JAVASCRIPT代码:
arrayjson = function(ele)
{
var item = ele;
return {"player":item[0], "age": item[1]};
}
d3.json('json.html', function (error, data){
var json_data;
if(error) { alert(error);visualizeit();}
data.map(arrayjson);
function tabulate(data, columns) {
var table = d3.select('body').append('table')
var thead = table.append('thead')
var tbody = table.append('tbody');
// append the header row
thead.append('tr')
.selectAll('th')
.data(columns).enter()
.append('th')
.text(function (column) { return column; });
// create a row for each object in the data
var rows = tbody.selectAll('tr')
.data(data)
.enter()
.append('tr');
// create a cell in each row for each column
var cells = rows.selectAll('td')
.data(function (row) {
return columns.map(function (column) {
return {column: column, value: row[column]};
});
})
.enter()
.append('td')
.text(function (d) { return d.value; });
return table;
}
// render the table(s)
tabulate(data, ['player', 'age']); // 2 column table
});
以及
HTML代码
<!DOCTYPE html>
<html>
<head>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="d3.min.js"></script>
<script type="text/javascript" src="c3.js"></script>
<link rel='stylesheet' href='style.css'>
</head>
<body>
<script type='text/javascript' src='pdisb.js'></script>
</body>
</html>
但是我无法弄清楚我犯了哪些错误。任何建议将不胜感激。它说“SyntaxError:JSON.parse:JSON数据后出现意外的非空白字符”