【问题标题】:How to save a multidimensional JSON array to a CSV file on Android?如何将多维 JSON 数组保存到 Android 上的 CSV 文件?
【发布时间】:2013-03-11 06:27:42
【问题描述】:

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

【问题讨论】:

标签: android json csv


【解决方案1】:

我从这里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-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多