Can not deserialize instance of Object out of STAR

2019-07-12 14:51发布

问题:

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;