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());
}
Reading JSON from a File Thankfully, Jackson makes this task as easy as the last one, we just provide the File to the readValue() method: final ObjectMapper objectMapper = new ObjectMapper(); List<Language> langList = objectMapper. readValue( new File("langs. json"), new TypeReference<List<Language>>(){}); langList.
Jackson JSON - Edit JSON DocumentreadAllBytes(Paths. get("employee. txt")); ObjectMapper objectMapper = new ObjectMapper(); //create JsonNode JsonNode rootNode = objectMapper. readTree(jsonData); //update JSON data ((ObjectNode) rootNode).
Stringify a JavaScript ArrayUse the JavaScript function JSON.stringify() to convert it into a string.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With