3

我想将大型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();
    }
}
4

1 回答 1

5

我从这里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"}]

我已将 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();
    }
}
于 2013-03-11T07:52:05.007 回答