【发布时间】:2018-11-25 07:22:03
【问题描述】:
我的应用数据文件夹中有一个 JSON 文件。
{
"user": [
{
"identifier": "1",
"name": "xyz",
"contact": [
{
"contact": "123"
},
{
"contact": "456"
}
]
}
]
}
现在我想像这样在运行时添加一个新用户。
{
"user": [
{
"identifier": "1",
"name": "xyz",
"contact": [
{
"contact": "123"
},
{
"contact": "456"
}
]
}
],
"user": [
{
"identifier": "2",
"name": "abc",
"contact": [
{
"contact": "445"
},
{
"contact": "789"
}
]
}
]
}
为此,我有这段代码,但它只是覆盖了现有用户。
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是一种写入文件的方法。
任何帮助将不胜感激。
【问题讨论】:
标签: android json android-studio