2013-03-11 46 views
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(); 
    } 
} 
+0

http://stackoverflow.com/questions/9561749/create-csv-fie-in-android-app看到此鏈接@swapnil – Venkat 2013-03-11 06:35:13

+0

@Venkat是的,我 – 2013-03-28 09:30:03

回答

5

我從這裏使用了opencsv-2.3.jar http://sourceforge.net/projects/opencsv/。這裏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(); 
    } 
} 
相關問題