2016-01-23 109 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")); 

重要了这一点:记得写月底关闭流