I have one JSON file in my app's data folder.
{
"user": [
{
"identifier": "1",
"name": "xyz",
"contact": [
{
"contact": "123"
},
{
"contact": "456"
}
]
}
]
}
Now I want to add a new user at runtime like this.
{
"user": [
{
"identifier": "1",
"name": "xyz",
"contact": [
{
"contact": "123"
},
{
"contact": "456"
}
]
}
],
"user": [
{
"identifier": "2",
"name": "abc",
"contact": [
{
"contact": "445"
},
{
"contact": "789"
}
]
}
]
}
For that, I have this code but it simply overrides the existing user.
String lastObject = getStringFronFile(new FileInputStream(file));
try {
JSONObject prevJSONObj = new JSONObject(lastObject);
prevJSONObj.put("sticker_packs",newUserArray);
writeJsonFile(file,prevJSONObj);
}
catch (Exception e) {
Log.d("Json Error", e.toString());
}
writeJsonFile is a method for writing in the file.
Any help will be appreciated.