我怎样才能达到这个使用jackson jsonNode并获得所需的输出(How can I achieve this using jackson jsonNode and get the desired output)
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(); }
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 i.e., First get the @totalCount from the above json string
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
{ "@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(); }
原文:https://stackoverflow.com/questions/41655924
最满意答案
你将C样式字符串(
char
指针)传递给std::istringstream
构造函数,它实际上实例化了一个std::string
并传递了它。 这是由于隐式转换而发生的。std::string
的转换构造函数将C样式字符串中的空字节字符解释为字符串终止符的结尾,从而导致忽略它后面的所有字符。为了避免这种情况,您可以显式构造一个
std::string
指定数据的大小并将其传递给std::istringstream
char bin[] = {0x30, 0xb, 0x0, 0x6, 0x6, 0x2b, 0xc, 0x89, 0x36, 0x84, 0x13, 0xa, 0x1}; std::istringstream strm(std::string(bin, sizeof(bin) / sizeof(bin[0])));
注意:我不确切知道你要完成什么,但我建议使用
std::vector
而不是原始字符缓冲区。What you pass a C style string (
char
pointer) to thestd::istringstream
constructor it actually instantiates astd::string
and passes that instead. This happens due to an implicit conversion. The conversion constructor ofstd::string
interprets a null byte character in C style strings as an end of string terminator causing all characters after it to be ignored.To avoid this you can explicitly construct a
std::string
specifying the size of the data and pass it tostd::istringstream
char bin[] = {0x30, 0xb, 0x0, 0x6, 0x6, 0x2b, 0xc, 0x89, 0x36, 0x84, 0x13, 0xa, 0x1}; std::istringstream strm(std::string(bin, sizeof(bin) / sizeof(bin[0])));
Note: I don't know exactly what you are trying to accomplish but I recommend using
std::vector
instead of a raw character buffer if possible.
相关问答
更多-
这对我有用: library(httr) url <- "ftp://arthurhou.pps.eosdis.nasa.gov/gpmdata/2014/04/01/imerg/3B-HHR.MS.MRG.3IMERG.20140401-S150000-E152959.0900.V03D.HDF5" output_file <- "3B-HHR.MS.MRG.3IMERG.20140401-S150000-E152959.0900.V03D.HDF5" my_email <- "someone@ex ...
-
是的,你可以在std::string嵌入空值。 例: std::string s; s.push_back('\0'); s.push_back('a'); assert(s.length() == 2); 注意: std::string的c_str()成员将总是在返回的字符缓冲区附加一个空字符; 但是, std::string的data()成员可能会或可能不会向返回的char缓冲区追加空字符。 小心操作员+ = 有一点需要注意的是在RHS上不要使用operator+=和char* 。 它只会加起来,直到 ...
-
一位同事为我提供了解决方法。 仍然不知道为什么查询忽略NULLS LAST,但我最终使用这个作为我的解决方法: String sql = "SELECT c FROM Contact c WHERE c.partnerCode =:pCode and c.activeFlag='Y'" +" ORDER BY nvl(c.primaryFlag, 'N') DESC, c.lastName ASC"; 注意:该列的值为'Y','N'或null。 使用nvl我使用'N'来代 ...
-
Java 8流与批处理(Java 8 Stream with batch processing)[2022-09-16]
您可以使用jOOλ (一种扩展Java 8流,用于单线程,顺序流用例)的库: Seq.seq(lazyFileStream) // Seq.zipWithIndex() // Seq > .groupBy(tuple -> tuple.v2 / 500) // Map > .forEach((index, batch) -> { ... -
你将C样式字符串( char指针)传递给std::istringstream构造函数,它实际上实例化了一个std::string并传递了它。 这是由于隐式转换而发生的。 std::string的转换构造函数将C样式字符串中的空字节字符解释为字符串终止符的结尾,从而导致忽略它后面的所有字符。 为了避免这种情况,您可以显式构造一个std::string指定数据的大小并将其传递给std::istringstream char bin[] = {0x30, 0xb, 0x0, 0x6, 0x6, 0x2b, 0xc ...
-
您可以使用StreamConverters实用程序从java.io流中获取Source和Sink 。 更多信息在这里 。 您可以使用flatMapConcat或flatMapMerge将Source s流flatMapMerge为单个流。 更多信息在这里 。 一个简单的例子可能是: val source: Source[String, NotUsed] = ??? def gimmeInputStream(name: String): InputStream = ??? val processi ...
-
我的理解是,使用像Storm这样的框架的目的是从应用程序/ Web服务器卸载繁重的处理(无论是cpu绑定,I / O绑定还是两者)并保持响应。 考虑每个应用程序服务器可能必须提供大量并发请求,而不是所有这些请求都与流处理有关。 如果应用服务器已经处理了大量事件,那么它可能构成较轻请求的瓶颈,因为服务器资源(认为CPU使用,内存,磁盘争用等)已经与更重的处理请求相关联。 如果您需要面对的实际负载不是那么重,或者只是通过添加应用服务器实例来处理,那么当然没有必要复杂化您的架构/拓扑,这实际上可能会减慢整个事情发 ...
-
减少或其可变变体collect ,始终是处理所有项目的操作。 您的操作可以通过自定义Spliterator ,例如 public static Stream
joinGroups(Stream s) { Spliterator sp=s.spliterator(); return StreamSupport.stream( new Spliterators.AbstractSpliterator (sp.estim ... -
在你的catch中,不要返回Observable.throw ,那么它应该按照需要继续流。 如果要将信息传播到外部流,可以使用return Observable.of("Error: Foo.Bar"); 例如。 或者在catch记录错误并返回一个Observable.empty()以使外部流基本上忽略该错误。 换句话说,只是链接这个: .catch(error => Rx.Observable.of(error)); const stream$ = Rx.Observable.range(1, 5) ...
-
嵌入null的字符串示例如下: var example = "This is a null: \0"; '\0'是Unicode字符'NULL'(U + 0000) 。 An example of a string with an embedded null is: var example = "This is a null: \0"; '\0' is the Unicode character 'NULL' (U+0000).