3

I want to store large JSON array data into a CSV file. How can I do this?

I have following code which do not save any data into "test1.csv" file created in my android "test" folder.

Here is the code.

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();
    }
}
1

1 Answer 1

5

I have used opencsv-2.3.jar from here http://sourceforge.net/projects/opencsv/. Documented here http://sourceforge.net/projects/opencsv/

I have following JSON with Boolean values which throws Boolean cant cast to string value

JSONArray outerArray = [{"value":true,"Id":0,"name":"214"},    {"value":true,"Id":0,"name":"215"},{"value":true,"Id":0,"name":"216"}]

i have changed json.get() to json.getString(). following code works fine now :)

    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();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.