java中的JSON解析来自文件

java中的JSON解析来自文件,java,json,jackson,Java,Json,Jackson,我正在做JSON编程的第一步。我有一个很大的json文件,其中包含以下书籍存档: 我用能手和二传手创造了POJO。现在我想查找给定作者所写的所有书籍: byte[] jsonData = Files.readAllBytes(Paths.get("archive.json")); ObjectMapper objectMapper = new ObjectMapper(); JsonNode rootNode = objectMapper.readTree(jsonData); JsonNo

我正在做JSON编程的第一步。我有一个很大的json文件,其中包含以下书籍存档:

我用能手和二传手创造了POJO。现在我想查找给定作者所写的所有书籍:

byte[] jsonData = Files.readAllBytes(Paths.get("archive.json"));
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(jsonData);

JsonNode authorNode = rootNode.path("Gary Cornell");
Iterator<JsonNode> elements = authorNode.elements();
while(elements.hasNext()){
    JsonNode isbn = elements.next();
    System.out.println(isbn.textValue());
}
byte[]jsonData=Files.readAllBytes(path.get(“archive.json”);
ObjectMapper ObjectMapper=新的ObjectMapper();
JsonNode rootNode=objectMapper.readTree(jsonData);
JsonNode authorNode=rootNode.path(“Gary Cornell”);
迭代器元素=authorNode.elements();
while(elements.hasNext()){
JsonNode isbn=elements.next();
System.out.println(isbn.textValue());
}

但可悲的是,我做错了什么。我的应用程序只需编写整个json。

您需要访问内部节点以比较作者值。您可能希望这样做:

byte[] jsonData = Files.readAllBytes(Paths.get("volumes.json"));
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(jsonData);
String authorName = "Joshua Bloch"; // author name to find
JsonNode items = rootNode.path("items");
Iterator<JsonNode> elements = items.elements();

while(elements.hasNext()){
    JsonNode isbn = elements.next();
    if(isbn.has("volumeInfo")) {
        JsonNode volumeInfo = isbn.path("volumeInfo");
        if(volumeInfo.has("authors")) {
            JsonNode authors = volumeInfo.path("authors");
            if(authors.toString().contains(authorName)) {
                // Print complete book JSON value
                System.out.println(isbn.toString());
            }
        }

    }
}
byte[]jsonData=Files.readAllBytes(path.get(“volumes.json”);
ObjectMapper ObjectMapper=新的ObjectMapper();
JsonNode rootNode=objectMapper.readTree(jsonData);
字符串authorName=“Joshua Bloch”//要查找的作者名称
JsonNode items=rootNode.path(“items”);
迭代器元素=items.elements();
while(elements.hasNext()){
JsonNode isbn=elements.next();
如果(isbn.has(“卷信息”)){
JsonNode volumeInfo=isbn.path(“volumeInfo”);
如果(volumeInfo.has(“作者”)){
JsonNode authors=volumeInfo.path(“authors”);
if(authors.toString().contains(authorName)){
//打印完整的图书JSON值
System.out.println(isbn.toString());
}
}
}
}

大多数jsonNode方法期望的是字段名而不是实际值

如果你在找ISBN,你可以这样做 (所以这可能不是最好的例子)

publicstaticvoidmain(字符串[]args)引发IOException{
byte[]jsonData=Files.readAllBytes(path.get(“archive.json”);
ObjectMapper ObjectMapper=新的ObjectMapper();
JsonNode rootNode=objectMapper.readTree(jsonData);
List volumeNodes=rootNode.findValue(“volumeInfo”);
字符串authorName=“Gary Cornell”;
for(JsonNode卷节点:卷节点){
JsonNode authors=volumeNode.path(“authors”);
迭代器authorIt=authors.elements();
while(authorIt.hasNext()){
JsonNode author=authorIt.next();
if(authorName.equals(author.textValue())){
System.out.println(author.textValue());
JsonNode isbn=卷节点路径(“行业标识符”);
迭代器isbnIt=isbn.elements();
while(isbnIt.hasNext()){
System.out.println(isbnIt.next());
}
}
}
}
}
    public static void main(String[] args) throws IOException {

        byte[] jsonData = Files.readAllBytes(Paths.get("archive.json"));
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode rootNode = objectMapper.readTree(jsonData);

        List<JsonNode> volumeNodes = rootNode.findValues("volumeInfo");
        String authorName = "Gary Cornell";

        for (JsonNode volumeNode : volumeNodes) {

            JsonNode authors = volumeNode.path("authors");
            Iterator<JsonNode> authorIt = authors.elements();

            while(authorIt.hasNext()){

                JsonNode author = authorIt.next();

                if (authorName.equals(author.textValue())) {

                    System.out.println(author.textValue());

                    JsonNode isbn = volumeNode.path("industryIdentifiers");
                    Iterator<JsonNode> isbnIt = isbn.elements();

                    while(isbnIt.hasNext()) {
                        System.out.println(isbnIt.next());
                    }
                }
            }
        }
    }