本文介绍了无法从START_ARRAY令牌中反序列化Object的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个对象,一个是Dashboard,第二个是Room我有一个json,看起来像这样
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"
}
]
}
对象是
public class DashBoard {
private int hotel_id;
private int hotel_room;
@JsonProperty("hotel_properties")
private Room hotel_properties;
}
还有另一个Object Room,看起来像这样
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;
}
我隐藏Stackoverflow的所有构造函数,setter和getter但是它在我的code
i想要使用此代码从URL使用ObjectMapper将Json解析为Object
I am Hide all constructor,setter and getter for Stackoverflow but it is in my codei 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();
}
但我收到此错误
org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of Object out of START_ARRAY token
请帮我解决这个问题
推荐答案
来自 JSON
您发布的字符串,看起来有一个 Room
对象列表。但是您使用了一个对象。
From the JSON
String you posted, it looks like there is a list of Room
objects. But you have used a single object.
在 DashBoard
类中,尝试更改:
private Room hotel_properties;
to:
private List<Room> hotel_properties;
这篇关于无法从START_ARRAY令牌中反序列化Object的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!