我將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();
}
}
}
所以,我怎麼能添加的東西到我家串?
有很多圖書館處理JSON,從谷歌檢查gson,它會使處理JSON更容易 –