Reputation: 7613
I've the following code that returns json object. And I need to filter sender email, subject, and creationDate. The code does the job but I felt like there is an efficient way to do it. I appreciate your suggestion.
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");
}
Here is the json object I receive from MS Office API.
{"@odata.context":"https://graph.microsoft.com/v1.0/$metadata#users('user34.onmicrosoft.com')/messages(sender,subject,createdDateTime)","value":[{"@odata.etag":"W/\”sljkasfdiou7978klosadf\"","id”:"lkjasdfu97978KLJASDFS_WGHJJ76J897DKdcuvtymBTItq836K34PUAAAvoK3SAAA=","createdDateTime":"2016-08-27T04:07:08Z","subject":"View your 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":"Order confirmation: Thank you for your purchase","sender":{"emailAddress":{"name":"Microsoft Online Services Team","address":"[email protected]"}}},{"@odata.etag":"W/\”LJKOIU987983\"","id”:”ladjksflk83l.x8783LKFW3=","createdDateTime":"2016-06-24T03:03:26Z","subject":"Attention: Your Microsoft Azure Active Directory Premium trial subscription will be disabled soon","sender":{"emailAddress":{"name":"Microsoft Online Services Team","address":"[email protected]"}}}]}
Upvotes: 0
Views: 1504
Reputation: 59358
By default Office 365 REST API response payload also includes common annotations such as:
odata.context
: the context URL of the payloadodata.etag
: the ETag of the entity, as appropriateThe below picture demonstrates it
As you've already might guessed it could be controlled via odata.metadata
parameter:
The
odata.metadata parameter
can be applied to the Accept header of an OData request to influence how much control information will be included in the response.
Example (C# version)
The example demonstrates how to set odata.metadata=none
format parameter via Accept
header to indicate that the service SHOULD omit control information
using (var client = new HttpClient(handler))
{
var url = "https://outlook.office365.com/api/v1.0/me/messages?$select=Sender,Subject,DateTimeCreated";
client.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse(GetMediaType("none",false,false)));
var result = await client.GetStringAsync(url);
var data = JObject.Parse(result);
foreach (var item in data["value"])
{
//process item;
}
}
where
private static string GetMediaType(string metadata,bool streaming,bool IEEE754Compatible)
{
return String.Format("application/json; OData.metadata={0}; OData.streaming={1}; IEEE754Compatible={2}",metadata,streaming, IEEE754Compatible);
}
Upvotes: 2