Can not deserialize instance of Object out of START_ARRAY token

笑着哭i 提交于 2019-12-12 17:07:50

问题


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

  {
"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

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.jackson.map.JsonMappingException: Can not deserialize instance of Object out of START_ARRAY token

please help me out from this


回答1:


From the JSON String you posted, it looks like there is a list of Room objects. But you have used a single object.

In your DashBoard class, try changing:

private Room hotel_properties;

to:

private List<Room> hotel_properties;


来源:https://stackoverflow.com/questions/32795219/can-not-deserialize-instance-of-object-out-of-start-array-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!