在android中将csv格式转换为Json的示例。我在Converting an CSV file to a JSON object in Java找到了一个解决方案,但没有工作,或者我遗漏了任何东西。
提前致谢。
package com.example.readfilefromsdcard;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;
import org.codehaus.jackson.map.ObjectMapper;
import com.opencsv.bean.ColumnPositionMappingStrategy;
import com.opencsv.bean.CsvToBean;
import com.opencsv.bean.HeaderColumnNameMappingStrategy;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button btnWriteSDFile;
Button btnReadSDFile;
String path = "/sdcard/mydocs/test.csv";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnReadSDFile = (Button) findViewById(R.id.btnReadSDFile);
btnReadSDFile.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
ConvertCsvToJson(path,"TestJavaBeans");
// ConvertCsvToJson1(path);
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
public void ConvertCsvToJson(String path, String clsName) throws IOException, ClassNotFoundException {
String pathToCsvFile = path;
String javaBeanClassName = "" + clsName;
final File file = new File(pathToCsvFile);
if (!file.exists()) {
System.out.println("The file you specified does not exist. path=" + pathToCsvFile);
}
Class<?> type = null;
try {
type = Class.forName(javaBeanClassName);
} catch (ClassNotFoundException e) {
System.out.println("The java bean you specified does not exist. className=" + javaBeanClassName);
}
HeaderColumnNameMappingStrategy<TestJavaBeans> strat = new HeaderColumnNameMappingStrategy<TestJavaBeans>();
//strat.setType(type);
CsvToBean<TestJavaBeans> csv = new CsvToBean<TestJavaBeans>();
List<TestJavaBeans> list = csv.parse(strat, new InputStreamReader(new FileInputStream(file)));
System.out.println(new ObjectMapper().writeValueAsString(list));
}
public void ConvertCsvToJson1(String path) throws IOException, ClassNotFoundException {
final ColumnPositionMappingStrategy<TestJavaBeans > strategy = new ColumnPositionMappingStrategy<TestJavaBeans>();
strategy.setType(TestJavaBeans .class);
strategy.setColumnMapping(new String[] { "name", "id", });
final CsvToBean<TestJavaBeans > csvToBean = new CsvToBean<TestJavaBeans >();
final List<TestJavaBeans > beanExamples;
try {
final Reader reader = new FileReader(path);
beanExamples = csvToBean.parse(strategy, reader);
System.out.println(new ObjectMapper().writeValueAsString(beanExamples));
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}
package com.example.readfilefromsdcard;
public class TestJavaBeans {
private String name;
private String id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
参考图书馆:jackson-all-1.9.0.jar和opencsv-3.3.jar
log:找不到方法java.beans.Introspector.getBeanInfo,从方法com.opencsv.bean.HeaderColumnNameMappingStrategy.loadDescriptors
中引用答案 0 :(得分:1)
真的没有普及&#34;转换&#34;:JSON是一个层次结构,而CSV是平的。您需要为JSON对象建模并将CSV中的字段一次映射到JSON对象。它更像是一个逻辑问题而不是编码问题。
答案 1 :(得分:1)
我会建议这种方法
创建一个具有类变量List<TestJavaBeans> list
然后使用Jackson将这个“包装器”转换为json
答案 2 :(得分:0)
我首选将csv转换为json的链接如下: