2016-01-23 289 views
3

我將JSON文件存儲在資產文件夾中。從它讀取很容易,但我怎麼寫它,如果退出應用程序時保存的數據?將數據寫入本地JSON文件

JSON:

{ 
    "Home": [ 
     { 
      "Task": "Lapup1", 
      "Time": "14:00", 
      "Date": "26/12/2016" 
     }, 
     { 
      "Task": "Lapup2", 
      "Time": "17:00", 
      "Date": "26/12/2016" 
     }, 
     { 
      "Task": "Lapup3", 
      "Time": "15:00", 
      "Date": "26/12/2016" 
     } 
    ] 
} 

JSON分析器(讀):

public class JSONParser { 

    ArrayList<Task> taskList; 
    String json; 

    public JSONParser(Context context) { 
     taskList = new ArrayList<Task>(); 
     json = null; 
     try { 
      InputStream is = context.getAssets().open("Home.json"); 
      int size = is.available(); 
      byte[] buffer = new byte[size]; 
      is.read(buffer); 
      is.close(); 
      json = new String(buffer, "UTF-8"); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 
    } 

    public void getJsonData(String type) { 
     try { 
      JSONObject obj = new JSONObject(json); 
      JSONArray m_jArry = obj.getJSONArray("Home"); 

      for (int i = 0; i < m_jArry.length(); i++) { 
       JSONObject jo_inside = m_jArry.getJSONObject(i); 
       Log.d("DTAG", jo_inside.getString("Task")); 
       Log.d("DTAG", jo_inside.getString("Time")); 
       Log.d("DTAG", jo_inside.getString("Date")); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

所以,我怎麼能添加的東西到我家串?

+3

有很多圖書館處理JSON,從谷歌檢查gson,它會使處理JSON更容易 –

回答

4

你不能。資產是隻讀區域。要存儲更改,您必須將文件存儲在其他位置。

0

如果您希望您的Json對象是動態的,您可以(其中一個選項)
將其存儲在SharedPrefferences中,並通過修改進行編輯。

1

您無法將文件寫入資產文件夾。 資產文件夾是隻讀的

您需要執行的過程是將JSON文件從資產複製到外部文件存儲。只有這樣,您才能寫入JSON文件並保存它。

1)從資產中的文件複製到外部存儲:

private void copyAssets() { 
    AssetManager assetManager = getAssets(); 
    String[] files = null; 
    try { 
     files = assetManager.list(""); 
    } catch (IOException e) { 
     Log.e("tag", "Failed to get asset file list.", e); 
    } 
    if (files != null) for (String filename : files) { 
     InputStream in = null; 
     OutputStream out = null; 
     try { 
      in = assetManager.open(filename); 
      File outFile = new File(getExternalFilesDir(null), filename); 
      out = new FileOutputStream(outFile); 
      copyFile(in, out); 
     } catch(IOException e) { 
      Log.e("tag", "Failed to copy asset file: " + filename, e); 
     } finally { 
      if (in != null) { 
      try { 
       in.close(); 
      } catch (IOException e) { 
       // NOOP 
      } 
     } 
     if (out != null) { 
      try { 
       out.close(); 
      } catch (IOException e) { 
       // NOOP 
      } 
     } 
    } 
} 

private void copyFile(InputStream in, OutputStream out) throws IOException { 
    byte[] buffer = new byte[1024]; 
    int read; 
    while((read = in.read(buffer)) != -1){ 
     out.write(buffer, 0, read); 
    } 
} 

2)讀取存儲在外部JSON文件:

File JSONfile = new File(getExternalFilesDir(null).getPath(), "Home.json"); 

3)使用JsonWriter類寫在JSON上並保存文件。按照從Android開發者這個很好的鏈接:http://developer.android.com/reference/android/util/JsonWriter.html

實例保存:

OutputStream out = new FileOutputStream(JSONfile); 
writer = new JsonWriter(new OutputStreamWriter(out, "UTF-8")); 

重要了這一點:記得寫月底關閉流