简体   繁体   中英

Retrieve strings from JSON array using Jackson

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.)

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://stackoverflow.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.

(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.)

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());
        }

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

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).

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.

Hope this helps!

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 :

Query{
Pages{
Word[] Links;
}
}

Word{
String word;
String code;
}

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

If that is not attractive look here and try 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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