我正在使用以下代碼,但出現錯誤:JSON是undefined.Any線索?CSV-JSON轉換時出錯
<html>
<head>
<title>Demo - Covnert JSON to CSV</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="https://github.com/douglascrockford/JSON-js/raw/master/json2.js"></script>
<script type="text/javascript">
// JSON to CSV Converter
function ConvertToCSV(objArray) {
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
var str = '';
for (var i = 0; i < array.length; i++) {
var line = '';
for (var index in array[i]) {
if (line != '') line += ','
line += array[i][index];
}
str += line + '\r\n';
}
return str;
}
// Example
$(document).ready(function() {
// Create Object
var items = [
{
"items": [
{
"ID": "1",
"NAME": " John"
},
{
"ID": "2",
"NAME": " Ashley"
},
{
"ID": "3",
"NAME": " Joseph"
}
]
}];
// Convert Object to JSON
var jsonObject = JSON.stringify(items);
// Display JSON
$('#json').text(jsonObject);
// Convert JSON to CSV & Display CSV
$('#csv').text(ConvertToCSV(jsonObject));
});
</script>
</head>
<body>
<h1>
JSON</h1>
<pre id="json"></pre>
<h1>
CSV</h1>
<pre id="csv"></pre>
</body>
</html>
你使用什麼瀏覽器? – Eric
您的CSV轉換錯誤,它給出了[對象對象],[對象對象],[對象對象]。看到這裏的工作示例http://jsfiddle.net/sturtevant/vUnF9/ – Anand