无法从START_ARRAY标记中反序列化Object的实例

时间:2015-09-26 08:38:48

标签: java json jackson jsonobject object-object-mapping

我有两个对象,一个是仪表板,第二个是房间,我有一个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"
      }
      ] 
  }

对象是

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

还有另一个Object Room,看起来像这样

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;
}

我隐藏了Stackoverflow的所有构造函数,setter和getter,但它在我的代码中 我想使用此代码

从URL使用ObjectMapper将Json解析为Object
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();
        }

但我收到此错误

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

请帮助我解决这个问题

1 个答案:

答案 0 :(得分:1)

从您发布的JSON字符串中,看起来有一个Room对象列表。但是你使用了一个对象。

DashBoard课程中,尝试更改:

private Room hotel_properties;

为:

private List<Room> hotel_properties;