3
我想將大型JSON
陣列數據存儲到CSV文件中。我怎樣才能做到這一點?如何將多維JSON數組保存到Android上的CSV文件中?
我有下面的代碼不會將任何數據保存到我的android「測試」文件夾中創建的「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();
}
}
http://stackoverflow.com/questions/9561749/create-csv-fie-in-android-app看到此鏈接@swapnil – Venkat 2013-03-11 06:35:13
@Venkat是的,我 – 2013-03-28 09:30:03