I want to store chat history in a JSON Object, one object for each conversation, within this object i want to have an Array with all the messages.
Like this:
{"Channel_123":[
{"from":"john","to":"bill", "msg":"Hello", "time":"09:57"},
{"from":"bill","to":"john", "msg":"Hey John", "time":"09:58"}
]
}, {"Channel_234":[
{"from":"bob","to":"judy", "msg":"Hello", "time":"10:37"},
{"from":"judy","to":"bob", "msg":"Hey!", "time":"10:38"}
]
}
My current method looks like this: (the string channel contains the conversation id illustrated above with Channel_234
JSONObject obj = new JSONObject();
JSONObject msgObj = new JSONObject();
public void addMessage(String from, String to, String msg, String time, String channel) {
try {
msgObj.put("from", from);
msgObj.put("to", to);
msgObj.put("msg", msg);
msgObj.put("time", time);
obj.put(channel, array);
} catch (Exception ex) {
System.out.println(ex.getStackTrace());
logger.log(Level.SEVERE, "Exception: ", ex);
}
System.out.println(obj);
}
But for some reason the method is not appending to the object it is overwriting what was previously there.
Any ideas?
EDIT
I am using the simple json lib: json-simple-1.1.1.jar
obj.puttoobj.appendseems to have solved the problem!