将数据写入本地JSON文件

时间:2016-01-23 13:56:39

标签: android json

我将JSON文件存储在assets文件夹中。从中读取很容易,但是如何写入它以及退出应用程序时是否保存数据?

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 Parser(阅读):

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

那么如何在Home字符串中添加内容呢?

3 个答案:

答案 0 :(得分:5)

你不能。资产是一个只读区域。要存储更改,您必须将文件存储在其他位置。

答案 1 :(得分:1)

您无法将文件写入Assets文件夹。 Assets文件夹是只读的

您需要执行的步骤是将JSON文件从Assets复制到External Files Storage。只有这样,您才能写入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 Developer的这个好链接:http://developer.android.com/reference/android/util/JsonWriter.html

保存示例:

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

这一点非常重要:请记住在写作结束时关闭流

答案 2 :(得分:0)

如果你想要你的Json对象是动态的,你可以(其中一个选择)
将其存储在SharedPrefferences中并通过demend进行编辑。