我需要使用 java 更新我的 JSON 文件中的值,但不知何故我无法。以下是我试图解析的 JSON 格式
{
"recentActivities":[
{
"displayValue":"POC | Augmented Reality",
"link":"poc.jsp?search=augmented%20reality",
"timestamp":"18/07/2013 17:33"
},
{
"displayValue":"POC | Image Editing in Hybrid Application",
"link":"poc.jsp?search=image%20editing",
"timestamp":"18/07/2013 01:00"
}
],
"lastEmailSent": "29/06/2013 00:00"
}
我需要更新lastEmailSent
到当前日期,但不知何故我被卡住了。下面是我正在使用的java代码
private void updateLastEmailTimeStamp(String jsonFilePath) {
JSONParser parser = new JSONParser();
JSONObject jsonObject = new JSONObject();
JSONObject lastEmailTimeStamp = new JSONObject();
FileReader reader =null;
try {
File jsonFile = new File(jsonFilePath);
reader = new FileReader(jsonFile);
jsonObject = (JSONObject) parser.parse(reader);
lastEmailTimeStamp = (JSONObject) jsonObject.get("lastEmailSent");
//write current date as last mail sent time.
writeTimeStamp(lastEmailTimeStamp, jsonFile);
APP_LOGGER.info("last Email Sent timestamp updated");
} catch (IOException ex) {
APP_LOGGER.error(ex.getLocalizedMessage(), ex);
} catch (ParseException ex) {
APP_LOGGER.error(ex.getLocalizedMessage(), ex);
}
}
private void writeTimeStamp(JSONObject lastEmailTimeStamp, File jsonFile) {
FileWriter writer = null;
try{
writer = new FileWriter(jsonFile);
String currentDate = MyDateFormatterUtility.formatDate(new Date(),"dd/MM/yyyy HH:mm");
lastEmailTimeStamp.put(SubscriptionConstants.LAST_EMAIL_TIMESTAMP, currentDate);
writer.write(lastEmailTimeStamp.toJSONString());
}catch(IOException ex){
APP_LOGGER.error(ex.getLocalizedMessage(), ex);
}finally{
try {
writer.flush();
writer.close();
} catch (IOException ex) {
APP_LOGGER.error(ex.getLocalizedMessage(), ex);
}
}
}
我在以下行中收到错误
lastEmailTimeStamp = (JSONObject) jsonObject.get("lastEmailSent");
.
我想我没有正确解析或访问该对象。有人可以纠正我吗?谢谢!