[英]GSON.fromJson() issue with hashmap
我正在使用GSON 2.5。
我有2个字符串:
字符串ReservationRoomNightGood =“ [{\\” reservationRoomNightParams \\“:{\\” date \\“:\\” 2016-06-05 \\“,\\” totalPrice \\“:600,\\” currencyCode \\“:\\” EUR \\“,\\ “crsRatePlanId \\”:0,\\ “rateplanId \\”:0},\\ “roomNightExtra \\”:[]},{\\ “reservationRoomNightParams \\”:{\\ “日期\\”:\\ “2016年6月6日\\”, \\ “totalPrice \\”:400,\\ “CURRENCYCODE \\”:\\ “EUR \\”,\\ “crsRatePlanId \\”:0,\\ “rateplanId \\”:0},\\ “roomNightExtra \\”:[]}]“;
字符串ReservationRoomNightBad =“ [{\\” reservationRoomNightParams \\“:{\\” date \\“:\\” 2016-06-05 \\“,\\” totalPrice \\“:700,\\” currencyCode \\“:\\” EUR \\“,\\ “chRatePlanCodes \\”:{\\ “日期\\”:\\ “2016年6月5日\\”,\\ “totalPrice \\”:600,\\ “CURRENCYCODE \\”:\\ “EUR \\”,\\ “crsRatePlanId \\”:0, \\ “rateplanId \\”:0},\\ “crsRatePlanId \\”:12969,\\ “configuredCommission \\”:0,\\ “configuredCommissionType \\”:\\ “P \\”,\\ “rateplanId \\”:0},\\“roomNightExtra \\ “:[]}]”;
我有这个类来填充字符串到:
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
public class ReservationRoomNight implements Serializable{
private static final long serialVersionUID = -3697795946626268528L;
transient private static Logger logger = Logger.getLogger(Reservation.class);
private List<ReservationExtraCost> roomNightExtra;
private Map<String, String> reservationRoomNightParams;
private Map<String, String> chRatePlanCodes = new HashMap<String, String>();
private long crsRatePlanId;
private Float configuredCommission;
private String configuredCommissionType;
public ReservationRoomNight(String nightDate, Float totalPrice, String currencyCode) {
reservationRoomNightParams = new HashMap<String, String>();
chRatePlanCodes = new HashMap<String, String>();
}
public List<ReservationExtraCost> getRoomNightExtra() {
return roomNightExtra;
}
public void setRoomNightExtra(List<ReservationExtraCost> roomNightExtra) {
this.roomNightExtra = roomNightExtra;
}
public Map<String, String> getChRatePlanCodes() {
return chRatePlanCodes;
}
public void setChRatePlanCodes(Map<String, String> chRatePlanCodes) {
this.chRatePlanCodes = chRatePlanCodes;
}
public String getChannelCodes(String key) {
return chRatePlanCodes.get(key);
}
public long getCRSRatePlanId() {
return crsRatePlanId;
}
}
毫无疑问,reservationRoomNightGood字符串提取出来,但我不知道为什么reserveRoomNightBad失败。
Type listType = new TypeToken<ArrayList<ReservationRoomNight>>() {}.getType();
List<ReservationRoomNight> goodReservationRoomNight = new Gson().fromJson(reservationRoomNightGood, listType);
List<ReservationRoomNight> boodReservationRoomNight = new Gson().fromJson(reservationRoomNightBad, listType);
如果我从ReservationRoomNightBad字符串中取出chRatePlanCodes哈希图,则没有问题。 但是,当我将字符串的那部分留在其中时会出现问题。例如,尝试使用此字符串运行它(reservationRoomNightBad-哈希图)
字符串ReservationRoomNightBad =“ [{\\” reservationRoomNightParams \\“:{\\” date \\“:\\” 2016-06-05 \\“,\\” totalPrice \\“:700,\\” currencyCode \\“:\\” EUR \\“,\\ “crsRatePlanId \\”:12969,\\ “configuredCommission \\”:0,\\ “configuredCommissionType \\”:\\ “P \\”,\\ “rateplanId \\”:0},\\ “roomNightExtra \\”:[]}]“;
有任何想法吗?
您的代码:
private Map<String, String> reservationRoomNightParams;
只接受键字符串&&值字符串,但是您为该属性提供了一个对象:
"chRatePlanCodes":{
"date":"2016-06-05",
"totalPrice":600,
"currencyCode":"EUR",
"crsRatePlanId":0,
"rateplanId":0
}
使用以下方法解决此问题:
private Map<String, Object> reservationRoomNightParams;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.