简体   繁体   English

JsonPath:使用List成员将JSON解析为Java对象

[英]JsonPath: Parsing JSON into a Java object with List member

I am trying to parse the following JSON into Book class: 我试图将以下JSON解析为Book类:

{
  "inventory": {
      "book": {
          "title": "Database Systems",
           "authors": [
              {"name": "Sudarshan"},
              {"name": "Korth"},
              {"name": "Silberschatz"}
           ]
       }
  }
}

Book class which has JSON annotations for the fields and has the following definition: Book类,其中包含字段的JSON注释,并具有以下定义:

package com.example.books    

public class Book {

    @JsonProperty("title")
    private String title;

    @JsonProperty("authors")
    private List<Author> authors;

    public Book() {
        authors = new ArrayList<Author>();
    }
    // constructors, getters, setters

}     

public class Author {
    @JsonProperty
    private String name;

    // constructor, getters, setters...

}

I am trying to achieve this using com.jayway.jsonpath.JsonPath : 我试图使用com.jayway.jsonpath.JsonPath实现此com.jayway.jsonpath.JsonPath

Book book = JsonPath.read(inventoryJson.toString(), "$.inventory.book");

This results in the following exception: 这导致以下异常:

java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.example.books.Book

I have also tried: 我也尝试过:

Book book = JsonPath.parse(inventoryJson.toString()).read("$.inventory.book", Book.class);

This one now gives a different exception: 这个现在给出了一个不同的例外:

java.lang.RuntimeException: Can not find Array 'authors' field in class com.example.book.Book
    at net.minidev.json.writer.BeansMapper$Bean.startArray(BeansMapper.java:79)
    at net.minidev.json.parser.JSONParserBase.readMain(JSONParserBase.java:402)
    at net.minidev.json.parser.JSONParserBase.readObject(JSONParserBase.java:542)
    at net.minidev.json.parser.JSONParserBase.readFirst(JSONParserBase.java:297)
    at net.minidev.json.parser.JSONParserBase.parse(JSONParserBase.java:154)
    at net.minidev.json.parser.JSONParserString.parse(JSONParserString.java:58)
    at net.minidev.json.parser.JSONParser.parse(JSONParser.java:263)
    at net.minidev.json.JSONValue.parse(JSONValue.java:206)
    at com.jayway.jsonpath.spi.mapper.JsonSmartMappingProvider.map(JsonSmartMappingProvider.java:82)
    at com.jayway.jsonpath.internal.JsonContext.convert(JsonContext.java:192)
    at com.jayway.jsonpath.internal.JsonContext.read(JsonContext.java:158)

How do I make it work? 我如何使其工作?

Original classes: 原班:
http://pastebin.com/SAhipfxL http://pastebin.com/SAhipfxL
http://pastebin.com/vWfWsaqN http://pastebin.com/vWfWsaqN
http://pastebin.com/7tqhJfw3 http://pastebin.com/7tqhJfw3

Complete stack trace: http://pastebin.com/QWV89Hgj 完整的堆栈跟踪: http//pastebin.com/QWV89Hgj

I found series of mistakes in code from your question. 我在你的问题代码中发现了一系列错误。

Let's start from json itself: it is not well-formed in exactly that view, which you've provided. 让我们从json本身开始:它在你所提供的那个视图中并不完美。

It should be: 它应该是:

{
"inventory": {
    "books": {
        "title": "Database Systems",
        "authors": [
            {"name": "Sudarshan"},
            {"name": "Korth"},
            {"name": "Silberschatz"}
         ]
     }
 }
}

Notice also please, that it should be " books " instead of " book ". 另请注意,它应该是“ 书籍 ”而不是“ ”。

Next problem is that you've declared classes Book and Author as package local. 接下来的问题是您已将类BookAuthor声明为包本地。 This won't work. 这不行。 Consider making them public or public static at least. 至少考虑将它们publicpublic static

These fixes will solve your problem, for second statement: 这些修复将解决您的问题,第二个声明:

Book book = JsonPath.parse(inventoryJson.toString()).read("$.inventory.books", Book.class); - it should work properly. - 它应该正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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