我有下面的代码返回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