使用Jackson从JSON数组中检索字符串

时间:2021-09-09 18:04:55

I've found several answers that are close to what I'm trying to do, but not enough that I've been able to get it to work. I have a bunch of JSON that looks like this example (only actually several levels deeper and with hundreds of items at the level I want to access): {"query":{"pages":{"links":[{"word":"bobsyeruncle","code":4},{"word":"easyaspie","code":3}]}}}. I can't change the format; it's someone else's API. There's quite a lot of this that I don't need; in fact I only want something like an array of ["bobsyeruncle","easyaspie"]. (Or a List or whatever.)

我已经找到了一些与我想要做的事情很接近的答案,但还不够我能让它发挥作用。我有一堆看起来像这个例子的JSON(实际上只有几个层次,并且在我想访问的层次上有数百个条目):{“查询”:{“页面”:{“链接”:[{“单词”:“bobsyeruncle”,“代码”:4},{“单词”:“easpie”,“代码”:3}}}}}]}。我不能改变格式;这是别人的API。有很多我不需要的东西;事实上,我只想要一个“博比叔叔”、“易赛斯派”这样的数组。(或者是列表之类的。)

I experimented with a simpler version of the JSON that didn't have an array, and was able to access a single string easily using the rootNode.get("query").get("pages")... way described in https://*.com/questions/338586/a-better-java-json-library/338608#338608. But I haven't been able to get at the array that way. Most of the answers I've found here assume that I want to create a POJO like "Links" that includes both "word" and "code," which I don't. In order to access the strings I want, is it necessary to create something like a list of "Links" that include both "word" and "code" and then ignore "code"? That doesn't seem right.

我尝试了一个没有数组的更简单的JSON版本,并且能够使用rootnode轻松访问单个字符串。get(“query”).get(“pages”)…在338608年https://*.com/questions/338586/a-better-java-json-library/338608方式描述。但是我还没能那样得到这个数组。我在这里找到的大多数答案都假设我想创建一个POJO,比如“Links”,它包含了“word”和“code”,但我没有。为了访问我想要的字符串,是否需要创建一个包含“word”和“code”的“Links”列表,然后忽略“code”?这似乎不正确的。

(Also, if anyone could point me to documentation/tutorials somewhere in between the JacksonInFiveMinutes tutorial and the whole javadoc, I'm sure that would help too.)

(另外,如果有人能在JacksonInFiveMinutes教程和整个javadoc之间找到文档/教程,我相信这也会有所帮助。)

ETA this worked, I think!:

埃塔:我想这行得通!

            String theJsonString = "{\"query\":{\"pages\":{\"links\":"
                + "[{\"word\":\"bobsyeruncle\"},{\"word\":\"easyaspie\"}]}}}";
        ObjectMapper mapper = new ObjectMapper();
        JsonNode rootNode = mapper.readTree(theJsonString);
        JsonNode interestingObjectNode = rootNode.path("query").path("pages").path("links");
        for (int i = 0; i < interestingObjectNode.size(); i ++) {
            System.out.println(interestingObjectNode.get(i).path("word").asText());
        }

2 个解决方案

#1


7  

Perhaps this blog entry might help: Traversing JSON trees with Jackson?

这个博客条目可能会有帮助:用Jackson遍历JSON树?

I am not sure which exact problem you have, but one thing to note is that JSON Arrays are traversed by passing index of entry, NOT the name. So whereas you would use objectNode.get("key"), you use arrayNode.get(0) instead. Or, if you want to play safe and allow "missing" entries, use arrayNode.path(0) (and ditto for JSON Objects).

我不确定您遇到了什么确切的问题,但是需要注意的是JSON数组是通过传递条目索引(而不是名称)来遍历的。你可以使用objectNode.get(“key”),你可以使用arrayNode.get(0)。或者,如果您希望确保安全并允许“丢失”条目,请使用arrayNode.path(0) (JSON对象也是如此)。

Also remember that you can go back between JSON Trees (JsonNode) and POJOs; ObjectMapper has multiple methods for converting between representations (convertValue(), readAsTree(), treeToValue(), valueToTree()). So it is possible to use data-binding for some parts, tree model for others; sometimes binding sub-trees as POJOs, other times just data-binding high-level and accessing sub-trees using tree model. This is a very powerful way to do things, but takes a while getting used to.

还要记住,可以回到JSON树(JsonNode)和pojo之间;ObjectMapper有多种方法在表示之间进行转换(convertValue()、readAsTree()、treeToValue()、valueToTree()))。因此,可以对某些部分使用数据绑定,对其他部分使用树模型;有时将子树绑定为pojo,有时只是数据绑定高级,并使用树模型访问子树。这是一种非常强大的方法,但是需要一段时间才能习惯。

Hope this helps!

希望这可以帮助!

#2


0  

In Google's GSON if you create a POJO that lacks some properties, then the coresponding JSON is ignored. It populates only those properties that have matching names. Why not create a class like this :

在谷歌的GSON中,如果您创建的POJO缺少一些属性,那么将忽略coresponding JSON。它只填充具有匹配名称的属性。为什么不创建这样的类呢:

Query{
Pages{
Word[] Links;
}
}

Word{
String word;
String code;
}

and then use LambdaJ to avoid writing all the loops to get the words?

然后使用LambdaJ避免写出所有的循环来获取单词?

If that is not attractive look here and try JSONPath

如果这不是吸引人的,看看这里,试试JSONPath

Lots of document databases out there like MongoDB and RavenDB etc use JSON as their format for storage. Querying complex JSON is built into them, use the same libraries that they are using.

许多文档数据库,如MongoDB和RavenDB等,都使用JSON作为它们的存储格式。查询复杂的JSON是内置的,使用它们使用的库。

#1


7  

Perhaps this blog entry might help: Traversing JSON trees with Jackson?

这个博客条目可能会有帮助:用Jackson遍历JSON树?

I am not sure which exact problem you have, but one thing to note is that JSON Arrays are traversed by passing index of entry, NOT the name. So whereas you would use objectNode.get("key"), you use arrayNode.get(0) instead. Or, if you want to play safe and allow "missing" entries, use arrayNode.path(0) (and ditto for JSON Objects).

我不确定您遇到了什么确切的问题,但是需要注意的是JSON数组是通过传递条目索引(而不是名称)来遍历的。你可以使用objectNode.get(“key”),你可以使用arrayNode.get(0)。或者,如果您希望确保安全并允许“丢失”条目,请使用arrayNode.path(0) (JSON对象也是如此)。

Also remember that you can go back between JSON Trees (JsonNode) and POJOs; ObjectMapper has multiple methods for converting between representations (convertValue(), readAsTree(), treeToValue(), valueToTree()). So it is possible to use data-binding for some parts, tree model for others; sometimes binding sub-trees as POJOs, other times just data-binding high-level and accessing sub-trees using tree model. This is a very powerful way to do things, but takes a while getting used to.

还要记住,可以回到JSON树(JsonNode)和pojo之间;ObjectMapper有多种方法在表示之间进行转换(convertValue()、readAsTree()、treeToValue()、valueToTree()))。因此,可以对某些部分使用数据绑定,对其他部分使用树模型;有时将子树绑定为pojo,有时只是数据绑定高级,并使用树模型访问子树。这是一种非常强大的方法,但是需要一段时间才能习惯。

Hope this helps!

希望这可以帮助!

#2


0  

In Google's GSON if you create a POJO that lacks some properties, then the coresponding JSON is ignored. It populates only those properties that have matching names. Why not create a class like this :

在谷歌的GSON中,如果您创建的POJO缺少一些属性,那么将忽略coresponding JSON。它只填充具有匹配名称的属性。为什么不创建这样的类呢:

Query{
Pages{
Word[] Links;
}
}

Word{
String word;
String code;
}

and then use LambdaJ to avoid writing all the loops to get the words?

然后使用LambdaJ避免写出所有的循环来获取单词?

If that is not attractive look here and try JSONPath

如果这不是吸引人的,看看这里,试试JSONPath

Lots of document databases out there like MongoDB and RavenDB etc use JSON as their format for storage. Querying complex JSON is built into them, use the same libraries that they are using.

许多文档数据库,如MongoDB和RavenDB等,都使用JSON作为它们的存储格式。查询复杂的JSON是内置的,使用它们使用的库。