[英]Using Java - Parsing REST response
我收到來自 Alfresco 的 REST 響應,如下所示:
{
"list": {
"pagination": {
"count": 2,
"hasMoreItems": false,
"totalItems": 2,
"skipCount": 0,
"maxItems": 100
},
"entries": [
{
"entry": {
"createdAt": "2021-07-28T16:01:52.607+0000",
"isFolder": false,
"isFile": true,
"createdByUser": {
"id": "admin",
"displayName": "Administrator"
},
"modifiedAt": "2021-07-28T16:01:53.616+0000",
"modifiedByUser": {
"id": "admin",
"displayName": "Administrator"
},
"name": "site1-export.zip",
"id": "9591936a-82ac-4acc-a75c-29dbaffcae77",
"nodeType": "cm:content",
"content": {
"mimeType": "application/zip",
"mimeTypeName": "ZIP",
"sizeInBytes": 22089,
"encoding": "UTF-8"
},
"parentId": "9b211145-5359-4d69-9cbc-7247ba2def21"
}
},
{
"entry": {
"createdAt": "2021-07-27T15:58:20.655+0000",
"isFolder": false,
"isFile": true,
"createdByUser": {
"id": "admin",
"displayName": "Administrator"
},
"modifiedAt": "2021-07-27T15:58:20.655+0000",
"modifiedByUser": {
"id": "admin",
"displayName": "Administrator"
},
"name": "zzTestPdf.pdf",
"id": "58833967-6c26-448a-8be1-9387312028e1",
"nodeType": "customprefixemp:employee_custom_type",
"content": {
"mimeType": "application/pdf",
"mimeTypeName": "Adobe PDF Document",
"sizeInBytes": 8037,
"encoding": "UTF-8"
},
"parentId": "9b211145-5359-4d69-9cbc-7247ba2def21"
}
}
]
}
}
這是我如何使用 jayway
ResponseEntity<String> response = restTemplate.exchange(theUrl, HttpMethod.GET, entity, String.class);
JSONObject obj_JSONObject = new JSONObject(response.getBody()).getJSONObject("list");
Configuration cf = Configuration.builder().options(Option.SUPPRESS_EXCEPTIONS).build();
DocumentContext ctx = JsonPath.using(cf).parse(obj_JSONObject.toString());
使用 jayway 我得到 ids 並像下面這樣迭代:
List<String> objListItemsId = ctx.read("$.entries[*].entry.id");
for (String fileId : objListItemsId) {
logger.info("FileID - " + fileId);
//logger.info("What should I do here??: " + fileId + "_" + fileName + "_" + nodeType + "_" + mimeType + "_" + mimeTypeName);
}
問題 1:我還需要其他詳細信息。 那么,如何獲取其他參數的值,如 - name
、 nodeType
、 mimeType
和mimeTypeName
? 我想在一行中為每個entry
打印這些值。 我願意接受任何解決方案。
更新:現在,我正在使用這種(骯臟的)方式:
List<String> objListItemsId = ctx.read("$.entries[*].entry.id");
List<String> objListItemsName = ctx.read("$.entries[*].entry.name");
List<String> objListItemsNodeType = ctx.read("$.entries[*].entry.nodeType");
List<String> objListItemsContent = ctx.read("$.entries[*].entry.content.mimeType");
for (int listlen = 0; listlen < objListItemsId.size(); listlen ++) {
System.out.println(objListItemsId.get(listlen) + "," + objListItemsName.get(listlen)+ "," +objListItemsNodeType.get(listlen) + "," + objListItemsContent.get(listlen));
}
問題2:有沒有更好的方法來實現上述目標?
雖然我不知道ctx
的類型,但肯定有更好的方法來做到這一點,但我假設它是某種 serde 庫。 大多數 serde 庫允許您反序列化為給定的對象。 要改進您的解決方案:
Entry
的類。 它看起來像:public class Entry {
String createdAt;
String name;
String id;
// add other members from API
}
//this will depend on serde library your are using. Read its docs.
List<Entry> objListItemsId = ctx.read("$.entries[*].entry", Entry.class);
它將在Entry
實例中反序列化您想要的所有內容。
更新
為了進一步簡化您的分析,你就可以擺脫的entry
你的內entries
陣列。 所以,而不是擁有
"entries": [
{
"entry": {
你可以有
"entries": [
{
// entry 1 details
},
{
// entry 2 details
}
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.