从此 JSON 对象获取字段的有效方法是什么?
What is the efficient way to get the fields from this JSON object?
我有以下 returns 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/$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”:"T45763@email.microsoftonline.com"}}},{"@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":"obue733@email.microsoftonline.com"}}},{"@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":"635cdeee@email.microsoftonline.com"}}}]}
默认情况下,Office 365 REST API 响应负载还包括常见的 annotations,例如:
odata.context
: payload 的上下文URL
odata.etag
:实体的ETag,视情况而定
下图为证
正如您可能已经猜到的那样,它可以通过 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.
示例(C# 版本)
示例演示了如何通过Accept
header设置odata.metadata=none
格式参数到表示服务应该省略控制信息
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;
}
}
哪里
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);
}
我有以下 returns 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/$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”:"T45763@email.microsoftonline.com"}}},{"@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":"obue733@email.microsoftonline.com"}}},{"@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":"635cdeee@email.microsoftonline.com"}}}]}
默认情况下,Office 365 REST API 响应负载还包括常见的 annotations,例如:
odata.context
: payload 的上下文URL
odata.etag
:实体的ETag,视情况而定
下图为证
正如您可能已经猜到的那样,它可以通过 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.
示例(C# 版本)
示例演示了如何通过Accept
header设置odata.metadata=none
格式参数到表示服务应该省略控制信息
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;
}
}
哪里
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);
}