繁体   English   中英

将数据写入本地JSON文件

[英]Write data to local JSON file

我有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字符串中添加一些内容呢?

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

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

您需要执行的过程是将JSON文件从Assets复制到外部文件存储。 只有这样,您才能写入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"));

这一点很重要: 记得在写作结束时关闭流

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM