Json回应
{
"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"
}
}
]
}
}
现在,我想通过遍历这个json响应来检查两个条件,即,首先从上面的json字符串中获取@totalCount
第二个条件,遍历每个文件节点并查看
if(myrepoName.equals(json response repository name) 然后从链接节点获取存储库的内容URL。这是
{
"@rel": "content",
"$": "content.url" <---
}
我的代码:
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();
}
答案 0 :(得分:0)
首先,JSON节点filesNode
不是JSON对象。正如您在示例中所见,此节点具有结构"file": [ ... ]
,这是一个JSON数组。所以你需要更改if语句,如果files节点是一个数组,那么继续:
// wrong
if (filesNode.isObject()) {
...
}
// correct
if (filesNode.isArray()) {
...
}
其次,您需要确定JSON对象repository
中的JSON节点file
以检查存储库的名称是否正确:
//
// },
// "repository": {
// "$": "VZMOBILE_13psb6j"
// },
//
String repoName = fileNode.at("/repository/$").asText();
if (repoName.equals(targetRepoName)) {
第三,您需要确定JSON对象link
中的JSON节点file
以查找内容链接。
for (JsonNode linkNode : linksNode) {
if ("content".equals(linkNode.get("@rel").asText())) {
return linkNode.get("$").asText();
}
}
这是整个代码。
// ...
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();
}
}
}
}
}