2016-01-14 41 views
1

我正在使用GSON 2.5。GSON.fromJson()問題與散列圖

我有2個字符串:

字符串reservationRoomNightGood = 「[{\」 reservationRoomNightParams \ 「:{\」 日期\ 「:\」 2016年6月5日\」,\ 「totalPrice \」 :600,\ 「CURRENCYCODE \」:\ 「EUR \」,\ 「crsRatePlanId \」:0,\ 「rateplanId \」:0},\ 「roomNightExtra \」:[]},{\ 「reservationRoomNightParams \」:{ \ 「日期\」:\ 「2016年6月6日\」,\ 「totalPrice \」:400,\ 「CURRENCYCODE \」:\ 「EUR \」,\ 「crsRatePlanId \」:0,\ 「rateplanId \」: 0},\ 「roomNightExtra \」:[]}]「;

字符串reservationRoomNightBad = 「[{\」 reservationRoomNightParams \ 「:{\」 日期\ 「:\」 2016年6月5日\ 「\ 」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字符串提取沒有什麼問題,但我不明白,爲什麼reservationRoomNightBad失敗。

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 - HashMap中)

字符串reservationRoomNightBad = 「[{\」 reservationRoomNightParams \「: {\ 「日期\」:\ 「2016年6月5日\」,\ 「totalPrice \」:700,\ 「CURRENCYCODE \」:\ 「EUR \」,\ 「crsRatePlanId \」:12969,\ 「configuredCommission \」 :0,\ 「configuredCommissionType \」:\ 「P \」,\ 「rateplanId \」:0},\ 「roomNightExtra \」:[]}]「;

任何想法?

回答

1

代碼:

private Map<String, String> reservationRoomNightParams; 

只接受密鑰字符串& &值字符串,但你給一個對象來此屬性:

private Map<String, Object> reservationRoomNightParams; 
:此使用

"chRatePlanCodes":{ 
       "date":"2016-06-05", 
       "totalPrice":600, 
       "currencyCode":"EUR", 
       "crsRatePlanId":0, 
       "rateplanId":0 
      } 

修復