Java hashmap的GSON.fromJson()问题
我用的是GSON 2.5 我有两个字符串: 字符串保留RoomNightGood= “[{reservationRoomNightParams\”:{date\”:“2016-06-05\”,“totalPrice\”:600,\“currencyCode\”:“EUR\”,“crsRatePlanId\”:0,\“rateplanId\”:0},\“roomNightExtra\”:[],{“reservationRoomNightParams\”:“date\”:“2016-06-06\”,“totalPrice\”,“totalPrice\”:400,\“currencyCode\”:“EUR\”,“rateplanId\”:“crsRatePlanId\”:::[],“CrsRoomNightPlanId\”:[],:::[],::[];0} 字符串reservationRoomNightBad= “[{”reservationRoomNightParams\“:{”日期“:“2016-06-05\”,“总价”:700,““汇率代码”:“欧元”,““chRatePlanCodes\”:{”日期“:“2016-06-05\”,“总价”:600,““汇率代码”:“欧元”,““汇率计划ID”:0,““汇率计划ID”:0},““汇率计划ID”:12969,““配置任务”:“汇率计划ID”:0,“:”类型:“:“计划ID:”0roomNightExtra\“:[]}]” 我有一个类来填充字符串:Java hashmap的GSON.fromJson()问题,java,hashmap,gson,Java,Hashmap,Gson,我用的是GSON 2.5 我有两个字符串: 字符串保留RoomNightGood= “[{reservationRoomNightParams\”:{date\”:“2016-06-05\”,“totalPrice\”:600,\“currencyCode\”:“EUR\”,“crsRatePlanId\”:0,\“rateplanId\”:0},\“roomNightExtra\”:[],{“reservationRoomNightParams\”:“date\”:“2016-06-06\”,
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;
}
}
import java.io.Serializable;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入org.apache.log4j.Logger;
公共类ReservationRoomNight实现可序列化{
私有静态最终长serialVersionUID=-36977959466268528L;
瞬态专用静态记录器=Logger.getLogger(Reservation.class);
私人名单;
私人地图预订RoomNightParams;
私有映射chRatePlanCodes=newhashmap();
私人长平面;
专用浮点数配置的委员会;
配置的专用字符串CommissionType;
公共预订RoomNight(字符串nightDate、浮动总价、字符串currencyCode){
reservationRoomNightParams=新建HashMap();
chRatePlanCodes=newhashmap();
}
公共列表getRoomNightExtra(){
返回房间晚上额外;
}
公共无效设置roomNightExtra(列出roomNightExtra){
this.roomNightExtra=roomNightExtra;
}
公共地图getChRatePlanCodes(){
返回chrateplancode;
}
公共无效setChRatePlanCodes(地图chRatePlanCodes){
this.chRatePlanCodes=chRatePlanCodes;
}
公共字符串getChannelCodes(字符串键){
返回chRatePlanCodes.get(key);
}
公共长getCRSRatePlanId(){
返回crsRatePlanId;
}
}
提取reservationRoomNightGood字符串没有问题,但我不明白reservationRoomNightBad失败的原因
Type listType = new TypeToken<ArrayList<ReservationRoomNight>>() {}.getType();
List<ReservationRoomNight> goodReservationRoomNight = new Gson().fromJson(reservationRoomNightGood, listType);
List<ReservationRoomNight> boodReservationRoomNight = new Gson().fromJson(reservationRoomNightBad, listType);
Type listType=new-TypeToken(){}.getType();
List goodReservationRoomNight=new Gson().fromJson(reservationRoomNightGood,listType);
List boodReservationRoomNight=new Gson().fromJson(reservationRoomNightBad,listType);
如果我从reservationRoomNightBad字符串中取出chRatePlanCodes hashmap,我没有问题。但是,当我将字符串的这一部分保留在中时,会出现问题。例如,尝试使用以下字符串运行它(reservationRoomNightBad-hashmap)
字符串reservationRoomNightBad=
“[{reservationRoomNightParams\”:{date\”:“2016-06-05\”、“totalPrice\”:700、“currencyCode\”:“EUR\”、“crsRatePlanId\”:12969、“configuredCommission\”:0、“configuredCommissionType\”:“P\”、“rateplanId\”:0}、“roomNightExtra\:[]”
有什么想法吗?您的代码:
private Map<String, String> reservationRoomNightParams;
使用以下方法修复此问题:
private Map<String, Object> reservationRoomNightParams;
private Map reservationRoomNightParams;
private Map<String, Object> reservationRoomNightParams;