简体   繁体   English

我如何使用杰克逊jsonNode实现这一点并获得所需的输出

[英]How can I achieve this using jackson jsonNode and get the desired output

Json response 杰森回应

{
    "searchResults": {
        "@xmlns": {
            "dvi": "http://localhsot.com/ns/1.0",
            "$": "http://localhsot.com/ns/1.0",
            "a": "http://alternate.localhsot.com/ns/1.0"
        },
        "@totalCount": "2",
        "file": [
            {
                "link": [
                    {
                        "@rel": "file",
                        "$": "file.url"
                    },
                    {
                        "@rel": "content",
                        "$": "content.url"
                    },
                    {
                        "@rel": "repository",
                        "$": "repository.url"
                    },
                    {
                        "@rel": "content.alternate",
                        "@a:width": "320",
                        "@a:height": "320",
                        "@a:imageFormat": "jpeg",
                        "@a:mimeType": "image/jpeg",
                        "@a:size": "-1",
                        "$": "some.url"
                    },
                    {
                        "@rel": "content.alternate",
                        "@a:width": "64",
                        "@a:height": "64",
                        "@a:imageFormat": "jpeg",
                        "@a:mimeType": "image/jpeg",
                        "@a:size": "-1",
                        "$": "some.url"
                    },
                    {
                        "@rel": "content.alternate",
                        "@a:width": "24",
                        "@a:height": "24",
                        "@a:imageFormat": "jpeg",
                        "@a:mimeType": "image/jpeg",
                        "@a:size": "-1",
                        "$": "some.url"
                    },
                    {
                        "@rel": "content.alternate",
                        "@a:width": "128",
                        "@a:height": "128",
                        "@a:imageFormat": "jpeg",
                        "@a:mimeType": "image/jpeg",
                        "@a:size": "-1",
                        "$": "some.url"
                    }
                ],
                "name": {
                    "$": "gullfoss.jpg"
                },
                "parentPath": {
                    "$": "/"
                },
                "size": {
                    "$": "91345"
                },
                "versionCreated": {
                    "$": "2017-01-13T16:38:20.059Z"
                },
                "systemAttribute": [
                    {
                        "@name": "Geo-Latitude",
                        "$": "47.6065"
                    },
                    {
                        "@name": "Geo-Longitude",
                        "$": "22.99514"
                    },
                    {
                        "@name": "Height",
                        "$": "579"
                    },
                    {
                        "@name": "Mime-Type",
                        "$": "image/jpeg"
                    },
                    {
                        "@name": "Timeline-Date",
                        "$": "-0091-03-27T04:26:40.000Z"
                    },
                    {
                        "@name": "Width",
                        "$": "680"
                    }
                ],
                "attributesChanged": {
                    "$": "false"
                },
                "deleted": {
                    "$": "false"
                },
                "repository": {
                    "$": "VZMOBILE_13psb6j"
                },
                "checksum": {
                    "$": "5d828c1d94b2569ff1bf60a9ebe9cd2de8ae8e6a9ad7d77d678c1766c78957c5"
                },
                "extension": {
                    "$": "jpg"
                },
                "fileAttribute": [
                    {
                        "@name": "ContentPermissions",
                        "$": "SHARE"
                    },
                    {
                        "@name": "Width",
                        "$": "680"
                    },
                    {
                        "@name": "CI_COMPLETE",
                        "$": "true"
                    },
                    {
                        "@name": "DRM_PROTECTED",
                        "$": "false"
                    },
                    {
                        "@name": "Height",
                        "$": "579"
                    }
                ],
                "contentToken": {
                    "$": "AHYLsb1sK9iy1Rn3yS4mQiM5pFL--AyTjY3i5G-zzdOm68QzB_GILrt1pXgfdEkUcPZZ5tUpn1Ih3j9wyfywIVY~"
                },
                "contentAccessible": {
                    "$": "true"
                },
                "contentChanged": {
                    "$": "false"
                }
            },
            {
                "link": [
                    {
                        "@rel": "file",
                        "$": "file.url"
                    },
                    {
                        "@rel": "content",
                        "$": "content.url"
                    },
                    {
                        "@rel": "repository",
                        "$": "repository.url"
                    },
                    {
                        "@rel": "content.alternate",
                        "@a:width": "320",
                        "@a:height": "320",
                        "@a:imageFormat": "jpeg",
                        "@a:mimeType": "image/jpeg",
                        "@a:size": "-1",
                        "$": "some.url"
                    },
                    {
                        "@rel": "content.alternate",
                        "@a:width": "64",
                        "@a:height": "64",
                        "@a:imageFormat": "jpeg",
                        "@a:mimeType": "image/jpeg",
                        "@a:size": "-1",
                        "$": "some.url"
                    },
                    {
                        "@rel": "content.alternate",
                        "@a:width": "24",
                        "@a:height": "24",
                        "@a:imageFormat": "jpeg",
                        "@a:mimeType": "image/jpeg",
                        "@a:size": "-1",
                        "$": "some.url"
                    },
                    {
                        "@rel": "content.alternate",
                        "@a:width": "128",
                        "@a:height": "128",
                        "@a:imageFormat": "jpeg",
                        "@a:mimeType": "image/jpeg",
                        "@a:size": "-1",
                        "$": "some.url"
                    }
                ],
                "name": {
                    "$": "gullfoss.jpg"
                },
                "parentPath": {
                    "$": "/Folder"
                },
                "size": {
                    "$": "91345"
                },
                "versionCreated": {
                    "$": "2017-01-13T16:38:59.728Z"
                },
                "systemAttribute": [
                    {
                        "@name": "Geo-Latitude",
                        "$": "47.6065"
                    },
                    {
                        "@name": "Geo-Longitude",
                        "$": "22.99514"
                    },
                    {
                        "@name": "Height",
                        "$": "579"
                    },
                    {
                        "@name": "Mime-Type",
                        "$": "image/jpeg"
                    },
                    {
                        "@name": "Timeline-Date",
                        "$": "-0091-03-27T04:26:40.000Z"
                    },
                    {
                        "@name": "Width",
                        "$": "680"
                    }
                ],
                "attributesChanged": {
                    "$": "false"
                },
                "deleted": {
                    "$": "false"
                },
                "repository": {
                    "$": "VZMOBILE_13psb6j"
                },
                "checksum": {
                    "$": "5d828c1d94b2569ff1bf60a9ebe9cd2de8ae8e6a9ad7d77d678c1766c78957c5"
                },
                "extension": {
                    "$": "jpg"
                },
                "fileAttribute": [
                    {
                        "@name": "ContentPermissions",
                        "$": "SHARE"
                    },
                    {
                        "@name": "Width",
                        "$": "680"
                    },
                    {
                        "@name": "CI_COMPLETE",
                        "$": "true"
                    },
                    {
                        "@name": "DRM_PROTECTED",
                        "$": "false"
                    },
                    {
                        "@name": "Height",
                        "$": "579"
                    }
                ],
                "contentToken": {
                    "$": "AHYLsb1sK9iy1Rn3yS4mQiM5pFL--AyTjY3i5G-zzdOm68QzB_GILrt1pXgfdEkUcPZZ5tUpn1Ih3j9wyfywIVY~"
                },
                "contentAccessible": {
                    "$": "true"
                },
                "contentChanged": {
                    "$": "false"
                }
            }
        ]
    }
}

Now, I want to check two condition by travesing this json response ie, First get the @totalCount from the above json string 现在,我想通过遍历此json响应来检查两个条件,即,首先从上述json字符串获取@totalCount

second condition, loop through each file node and see 第二种情况,遍历每个文件节点并查看

if(myrepoName.equals(json response repository name) then get the content url for repository from link node. which is this if(myrepoName.equals(json响应存储库名称)然后从链接节点获取存储库的内容URL。

{
    "@rel": "content",
    "$": "content.url"  <---
}

My Code: 我的代码:

try {
    JsonNode jsonNode = new ObjectMapper().readTree(response.prettyPrint());
    int totalCount = jsonNode.at("/searchResults/@totalCount").asInt();
    logger.info("Search returned " + totalCount + " results");
    if(totalCount > 0) {
        JsonNode filesNode = jsonNode.at("/searchResults/file");
        logger.info("Result: " + filesNode);
        if(filesNode.isObject()) {
            Iterator<JsonNode> filesIterator = filesNode.elements();
            while (filesIterator.hasNext())
            {
                // what i'm trying to get here is this
                JsonNode fileNode = filesIterator.next();
                Object name = fileNode.at("/$").asText();
                if (repoName.equals(name)) {
                    logger.info("Got it");
                    //get the content URL from link node
                    //missing code 
                    logger.info("Returning " + contentURL + " for repo " + repoName);
                    break;
                }
            }
        }
    }
} catch (IOException e) {
    e.printStackTrace();
}

First of all, the JSON node filesNode is not a JSON object. 首先,JSON节点filesNode不是JSON对象。 As you can find in your example, this node has structure "file": [ ... ] , which is a JSON array. 如您在示例中所发现的,此节点具有结构"file": [ ... ] ,这是一个JSON数组。 So you need to change the if-statement, if the files node is an array, then continue: 因此,如果文件节点是数组,则需要更改if语句,然后继续:

// wrong
if (filesNode.isObject()) {
    ...
}

// correct
if (filesNode.isArray()) {
    ...
}

Secondly, you need to exact the JSON node repository inside the JSON object file to check if the name of the repo is correct: 其次,您需要在JSON目标file精确定位JSON节点repository ,以检查repository的名称是否正确:

//
// },
// "repository": {
//     "$": "VZMOBILE_13psb6j"
// },
//
String repoName = fileNode.at("/repository/$").asText();
if (repoName.equals(targetRepoName)) {

Thirdly, you need to exact the JSON node link inside the JSON object file to find the content link. 第三,您需要在JSON对象file精确定位JSON节点link以找到内容链接。

for (JsonNode linkNode : linksNode) {
    if ("content".equals(linkNode.get("@rel").asText())) {
        return linkNode.get("$").asText();
    }
}

Here's the entire code. 这是完整的代码。

// ...
JsonNode filesNode = jsonNode.at("/searchResults/file");
if (filesNode.isArray()) {

  for (JsonNode fileNode : filesNode) {
    String repoName = fileNode.at("/repository/$").asText();
    if (repoName.equals(targetRepoName)) {

      for (JsonNode linkNode : fileNode.at("/link")) {
        if ("content".equals(linkNode.get("@rel").asText())) {
          contentURL = linkNode.get("$").asText();
        }
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM