我有下面的代碼返回json對象。我需要過濾發件人電子郵件,主題和creationDate。代碼完成這項工作,但我覺得有一個有效的方法來做到這一點。我很欣賞你的建議。從這個JSON對象獲取字段的有效方法是什麼?
ResponseEntity<String> response =
restTemplate.exchange(app.getResourceUrl() + personnelEmail+
MESSAGE+"/?$select=Sender,Subject,CreatedDateTime", HttpMethod.GET, request, String.class);
String str=response.getBody();
JSONObject jsonObject= new JSONObject(str);
JSONArray arrayList= (JSONArray)jsonObject.get("value");
List l=arrayList.toList();
for(int i=0;i<l.size();i++){
HashMap<String,HashMap> hashMap=(HashMap<String,HashMap>)l.get(i);
HashMap<String,HashMap> sender= hashMap.get("sender");
HashMap<String,String> senderEmail= sender.get("emailAddress");
String email= senderEmail.get("address");
}
這是我從MS Office API收到的json對象。
{ 「@ odata.context」: 「https://graph.microsoft.com/v1.0/ $元數據#用戶( 'user34.onmicrosoft.com')/消息(發送者,主題,createdDateTime)」, 「值」:[{「@的OData。 etag「:」W/\「sljkasfdiou7978klosadf \」「,」id「:」lkjasdfu97978KLJASDFS_WGHJJ76J897DKdcuvtymBTItq836K34PUAAAvoK3SAAA =「,」createdDateTime「:」2016-08-27T04:07:08Z「,」subject「:」查看 Office 365 Enterprise E3 billing「 statement」,「sender」:{「emailAddress」:{「name」:「Microsoft Online Services Team」,「address」:「[email protected]」}}},{「@ odata.etag 「:」W/\「JUU70303 \」「,」id「:」UEYO93988FK; O38GV3J884 =「,」createdDateTime「:」2016-08-26T15:28:47Z「,」subject「:」訂購 確認:謝謝爲您的 購買「,」se nder「:{」emailAddress「:{」name「:」Microsoft Online Services Team「,」address「:」[email protected]「}}},{」@ odata.etag「:」W/\ 「LJKOIU987983 \」「,」id「:」ladjksflk83l.x8783LKFW3 =「,」createdDateTime「:」2016-06-24T03:03:26Z「,」subject「:」注意: 您的Microsoft Azure Active Directory Premium試用訂閱將會 即將停用」, 「發件人」:{ 「EMAILADDRESS」:{ 「名」: 「微軟在線服務 隊」, 「地址」: 「[email protected]」}}}]}
我建議你花一些時間學習更快的XML jackson json解析器。尤其是http://www.cowtowncoder.com/blog/archives/2011/07/entry_458.html – Hector