我有这个JSONFile
{"PostsDetails":[
{
"pageInfo": {
"pagePic": "http://example.com/abc.jpg",
"pageName": "abc"
},
"id": 1,
"posts": [
{
"likesCount": "2",
"nameOfPersonWhoPosted": "Jane Doe",
"post_id": "0987654321_123456789012",
"timeOfPost": "0987654321",
"actor_id": "0987654321",
"message": "Can't wait to see it!",
"picOfPersonWhoPosted": "http://example.com/abc.jpg"
}
]
}
]}
我有这个方法通过id更新并将JSONObject写入文件。我如何通过id更新对象?我不知道如何做到这一点。我尝试从文件中删除旧对象,然后将新对象放入文件中,但我不希望将其附加到文件的最后部分。
" JSONObject帖子" on参数具有对象的更新值。
public Message updatePost(long id, JSONObject post) {
Message message = new Message("Failed", false);
if (!post.isEmpty()) {
try{
JSONObject jsonObject = fileUtil2.getFileJSONObject();
JSONArray arr = (JSONArray) jsonObject.get("PostsDetails");
Iterator itr = arr.iterator();
while (itr.hasNext()) {
JSONObject obj = (JSONObject) itr.next();
if (obj.get("id").equals(id)) {
//Not sure what to put here
}
if(fileUtil2.writeToFile(jsonObject)){
message.setMessage("Contact Updated.");
message.setStatus(true);
}
else{
message.setMessage("An error occured.");
message.setStatus(false);
}
}
}
catch(Exception e){
}
}
return message;
}