2012-10-27 73 views
0

我想將Foxpro 2.6 Dos數據文件(DBF - DBase III格式)轉換爲JSON文件。我們是否有任何C/C++庫將DBF轉換爲JSON以及從JSON轉換回DBF。將DBF轉換爲JSON

回答

1

Perl模塊DBD::XBase會幫助你。

在Linux上:sudo apt-get install libdbd-xbase-perl

在Windows上:安裝ActivePerl,然後ppm install DBD::XBase

之後,你將有命令行實用程序dbf_dumpdbfdump在Windows上),可以DBF文件轉換成CSV(您需要使用--fs ","開關創建CSV),然後您可以將CSV轉換爲JSON。但是,我會推薦學習Perl DBI的工作方式,以及編寫可以從DBF中讀取的代碼,就像從任何其他SQL/DBI源代碼一樣。您的Perl代碼可能如下所示:

use DBI; 

my $dbh = DBI->connect("DBI:XBase:/path/to/dbfs") 
    or die $DBI::errstr; 
my $sth = $dbh->prepare("SELECT * FROM mytable"); 
$sth->execute(); 
while (my $row = $sth->fetchrow_hashref()) { 
    # dump ($row->{field1}, $row->{field2}, ...) into your JSON file. 
    # you might want to use JSON::XS module here 
    # or, you can insert it into PostgreSQL or MySQL via DBI as well 
} 
$sth->finish(); 
$dbh->disconnect();