2013-07-22 212 views
0

我需要用java來更新我的JSON文件中的值,但不知何故,我無法to.Following是我試圖解析訪問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我的JSON格式到目前的日期,但不知何故我陷入困境。下面是我使用

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");我的Java代碼。

我想我沒有正確解析或訪問該對象。有人可以讓我正確嗎? 謝謝!

+0

什麼是錯誤? – DannyMo

+0

我無法實際追蹤錯誤。該行之後的調試器轉到某個線程類並停止。 –

+0

我假設你的方法拋出一個'RuntimeException',所以添加一個額外的'catch(Exception ex)'到你的try-catch來找出它是什麼 – DannyMo

回答

0

最後,我能找出解決方案。以下代碼需要更改:

private void updateLastEmailTimeStamp(String jsonFilePath) { 
    JSONParser parser = new JSONParser(); 
    JSONObject jsonObject = new JSONObject(); 

    FileReader reader =null; 
    try { 
     File jsonFile = new File(jsonFilePath); 
     reader = new FileReader(jsonFile); 
     jsonObject = (JSONObject) parser.parse(reader); 

     jsonObject.remove("lastEmailSent"); 


     //write current date as last mail sent time. 
     writeTimeStamp(jsonObject, 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); 
    } catch (RuntimeException e) { 
     e.printStackTrace(); 
    }catch (Exception e) { 
     e.printStackTrace(); 
    } 

} 


/** 
* Method to write current date as last mail sent timestamp 
* denoting when the newsletter was sent last. 
* 
* @param jsonObj- date for last email sent. 
* @param jsonFile - recentactivities.json file 
*/ 
@SuppressWarnings("unchecked") 
private void writeTimeStamp(JSONObject jsonObj, File jsonFile) { 
    FileWriter writer = null; 
    try { 
     writer = new FileWriter(jsonFile); 

     String currentDate = MyDateFormatter.formatDate(new Date(),"dd/MM/yyyy HH:mm"); 

     jsonObj.put("lastEmailSent", currentDate); 


     writer.write(jsonObj.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); 
     } 
    } 
} 
0

我同意@Hot舔,但你可以嘗試做修復它:

String lastEmailSent = jsonObject.getString("lastEmailSent"); 

而且,如果這不是問題,這可能是因爲從您的文件來的文字是不完全您在此發佈的JSON文本。在這種情況下,您可以將文件讀入字符串,添加斷點並檢查字符串以查看它是否包含您期望的所有JSON元素。

在Java 7中,可以像讀課文:

String content = readFile(jsonFilePath, Charset.defaultCharset()); 

static String readFile(String path, Charset encoding) 
    throws IOException 
{ 
     byte[] encoded = Files.readAllBytes(Paths.get(path)); 
     return encoding.decode(ByteBuffer.wrap(encoded)).toString(); 
} 
+0

即使在創建對象字符串後,我也無法成功覆蓋json對象的值 –

+0

好的,對不起。從你的問題來看,你完全不清楚你是否遇到了鑄造錯誤之外的麻煩。我很高興你明白了。 – Ted