我想将大型JSON
数组数据存储到CSV文件中.我怎样才能做到这一点?
我有以下代码,不会将任何数据保存到我的android"test"文件夹中创建的"test1.csv"文件中.
这是代码.
JSONArray outerArray = [{"value":true,"Id":0,"name":"214"}, {"value":true,"Id":0,"name":"215"},{"value":true,"Id":0,"name":"216"}]
public void saveCsv(JSONArray outerArray) throws IOException, JSONException {
String rootPath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/test/";
File dir = new File(rootPath);
if (!dir.exists()) {
dir.mkdir();
}
File file;
EditText editText = (EditText) findViewById(R.id.editText1);
if (!editText.getText().toString().equals("")) {
file = new File(rootPath, editText.getText().toString() + ".csv");
} else {
editText.setError("Defualt csv file name will be used");
Toast.makeText(this, "CSV name is empty", 5000).show();
file = new File(rootPath, "test1.csv");
}
file.createNewFile();
if (file.exists()) {
CSVWriter writer = new CSVWriter(new FileWriter(file), ',');
String[][] arrayOfArrays = new String[outerArray.length()][];
for (int i = 0; i < outerArray.length(); i++) {
JSONObject innerJsonArray = (JSONObject) outerArray.get(i);
String[] stringArray1 = new String[innerJsonArray.length()];
for (int j = 0; j < innerJsonArray.length(); j++) {
stringArray1[j] = (String) innerJsonArray.get("value");
}
arrayOfArrays[i] = stringArray1;
writer.writeNext(arrayOfArrays[i]);
}
writer.close();
}
}
Run Code Online (Sandbox Code Playgroud)
我从这里http://sourceforge.net/projects/opencsv/使用了opencsv-2.3.jar .记录在这里http://sourceforge.net/projects/opencsv/
我有跟随JSON的布尔值,它将布尔值转换为字符串值
JSONArray outerArray = [{"value":true,"Id":0,"name":"214"}, {"value":true,"Id":0,"name":"215"},{"value":true,"Id":0,"name":"216"}]
Run Code Online (Sandbox Code Playgroud)
我已将json.get()更改为json.getString().下面的代码工作正常:)
public void saveCsv(JSONArray outerArray) throws IOException, JSONException {
String rootPath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/test/";
File dir = new File(rootPath);
if (!dir.exists()) {
dir.mkdir();
}
File file;
EditText editText = (EditText) findViewById(R.id.editText1);
if (!editText.getText().toString().equals("")) {
file = new File(rootPath, editText.getText().toString() + ".csv");
} else {
editText.setError("Defualt csv file name will be used");
Toast.makeText(this, "CSV name is empty", 5000).show();
file = new File(rootPath, "test1.csv");
}
if(!file.exists()){
file.createNewFile();
}
if (file.exists()) {
CSVWriter writer = new CSVWriter(new FileWriter(file), ',');
String[][] arrayOfArrays = new String[outerArray.length()][];
for (int i = 0; i < outerArray.length(); i++) {
JSONObject innerJsonArray = (JSONObject) outerArray.get(i);
String[] stringArray1 = new String[innerJsonArray.length()];
stringArray1[0]= (String) innerJsonArray.getString("Id");
stringArray1[1]= (String) innerJsonArray.getString("value");
stringArray1[2]= (String) innerJsonArray.getString("name");
arrayOfArrays[i] = stringArray1;
writer.writeNext(arrayOfArrays[i]);
}
writer.close();
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2631 次 |
最近记录: |