Here is code in Android Studio in that I am creating a json object with username and password of one user and storing it to json array (just to have more users). In fact a am converting it to String before storing. So How can I read and store it back to json array that contains my json object?
Here is code
public void storeJson() {
jobj = new JSONObject();
try {
jobj.put("username",usern);
jobj.put("password",passwd);
} catch (JSONException e) {
e.printStackTrace();
}
jarray = new JSONArray();
jarray.put(jobj);
String str = jarray.toString();
try {
fileOutputStream = openFileOutput(passwordFile, 0);
fileWriter = new OutputStreamWriter(fileOutputStream);
try {
fileWriter.write(str);
fileWriter.flush();
fileWriter.close();
Toast.makeText(getApplicationContext(), "File Written", Toast.LENGTH_LONG).show();
username.setText("");
password.setText("");
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Write Error", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}