jQuery CSV 插件不会将行拆分为数组数组
我正在尝试使用 jQuery“CSV”插件,如下所示: http: //code.google.com/p/js-tables/wiki/CSV
根据文档:
// Convert CSV data into array of arrays
jQuery.csv()("1,2,3\n4,5,6\n7,8,9\n"); // = [ [1,2,3], [4,5,6], [7,8,9] ]
但是当我尝试做类似的事情时,它似乎只是将“\n”视为数组元素的中间。
我做错了什么? 我使用 jQuery 1.4.2 和 Firefox 3.5.10 进行测试。我也用 jQuery 1.3 尝试过并得到相同的结果。
如果是插件的问题,那么有人可以推荐另一个用于读取 CSV 的插件吗? 我的最终目标是将 CSV 从字符串转换为 HTML 表;我能找到的唯一一个专门要求 CSV 来自文件的插件,这对于我的任务来说是不可取的。
这是我放在一起的最小测试页,它说明它没有将行分成子数组:
<html>
<head>
<title>CSVtest</title>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="jquery.csv.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var lines = $.csv()("a,b\nc,d");
alert(lines[0][1]); // Displays: b
// c instead of the expected b
});
</script>
</head>
<body>
</body>
</html>
I am trying to use the jQuery "CSV" plugin, as documented here: http://code.google.com/p/js-tables/wiki/CSV
According to the documentation:
// Convert CSV data into array of arrays
jQuery.csv()("1,2,3\n4,5,6\n7,8,9\n"); // = [ [1,2,3], [4,5,6], [7,8,9] ]
But when I attempt to do something similar, it just seems to treat the "\n" as another character in the middle of an array element.
What am I doing wrong?
I am using jQuery 1.4.2 and Firefox 3.5.10 for testing. I have also tried it with jQuery 1.3 and get the same result.
If it is a problem with the plugin, then can someone suggest another plugin for reading CSV?
My ultimate goal is to convert CSV from a string into an HTML table; the only plugin I can find that specifically does this requires the CSV to come from a file, which is not desirable for my task.
Here is a minimal test page I put together which illustrates that it's not separating the lines into sub-arrays:
<html>
<head>
<title>CSVtest</title>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="jquery.csv.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var lines = $.csv()("a,b\nc,d");
alert(lines[0][1]); // Displays: b
// c instead of the expected b
});
</script>
</head>
<body>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保您使用的是该插件的最新版本。我使用 jQuery 1.4.2 和来自
http://js-tables.googlecode.com/svn/trunk/jquery.csv.min.js并且工作正常。http://jsfiddle.net/KRzzF/
在 IE8、IE6、Google Chrome 和 Firefox 中测试。我也测试了
一下,果然警报显示“d”。
Make sure you're using the latest version of the plugin. I copied and pasted your example using jQuery 1.4.2 and the (now outdated) CSV plugin from
http://js-tables.googlecode.com/svn/trunk/jquery.csv.min.jsand it works fine.http://jsfiddle.net/KRzzF/
Tested in IE8, IE6, Google Chrome and Firefox. I also tested
and sure enough, the alert displayed "d".
如果您只处理简单数据(无引号),您可以执行以下操作:
If you only deal with simple data (no quotation) you can do something like this: