Java 无法从 START_ARRAY 令牌反序列化对象的实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32795219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 13:06:28  来源:igfitidea点击:

Can not deserialize instance of Object out of START_ARRAY token

javajsonHymansonjsonobjectobject-object-mapping

提问by Ashish Srivastava

I have two object one is Dashboard and second is Room i have a json which is look like this

我有两个对象,一个是仪表板,第二个是 Room 我有一个 json,它看起来像这样

  {
"hotel_id":"1",
"hotel_room":"200",
"hotel_properties":[{
        "id":"1",
        "room_type":"Single",
        "rack_rate":"2000",
        "publish_rate":"1800",
        "discount":"10",
        "availiable":"40",
        "total":"50"
      },
      {
        "id":"2",
        "room_type":"Double",
        "rack_rate":"4000",
        "publish_rate":"3600",
        "discount":"10",
        "availiable":"45",
        "total":"50"
      }
      ] 
  }

And the Object is

对象是

public class DashBoard {
    private int hotel_id;
    private int hotel_room;
    @JsonProperty("hotel_properties")
    private Room hotel_properties;
    }

There is another Object Room which is look like this

还有另一个对象室看起来像这样

public class Room { 
    private Long id;
    private String room_type;
    private String rack_rate;
    private String publish_rate;
    private String discount;
    private String availiable;
    private String total;
}

I am Hide all constructor,setter and getter for Stackoverflow but it is in my code i want parse Json to Object using ObjectMapper from an URL using this code

我隐藏了 Stackoverflow 的所有构造函数、setter 和 getter,但在我的代码中,我想使用此代码从 URL 中使用 ObjectMapper 将 Json 解析为对象

JsonReader jsonReader = new JsonReader();
        ObjectMapper mapper = new ObjectMapper();
        try {
            JSONObject json =  jsonReader.readJsonFromUrl("http://localhost/quinchy/json/dashboard.json");
            DashBoard dsh = mapper.readValue(json.toString(), DashBoard.class); 
            System.out.println(json.toString());
        } catch (IOException | JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

but i get this error

但我收到这个错误

org.codehaus.Hymanson.map.JsonMappingException: Can not deserialize instance of Object out of START_ARRAY token

please help me out from this

请帮我解决这个问题

采纳答案by Amila

From the JSONString you posted, it looks like there is a list of Roomobjects. But you have used a single object.

JSON您发布的字符串来看,似乎有一个Room对象列表。但是您使用了单个对象。

In your DashBoardclass, try changing:

在您的DashBoard班级中,尝试更改:

private Room hotel_properties;

to:

到:

private List<Room> hotel_properties;