GSON.fromJson()问题与hashmap

时间:2016-01-14 16:48:06

标签: java hashmap gson

我使用的是GSON 2.5。

我有2个字符串:

  

String reservationRoomNightGood =   " [{\" reservationRoomNightParams \":{\"日期\":\" 2016年6月5日\" \&# 34; totalPrice \":600,\" CURRENCYCODE \":\" EUR \" \" crsRatePlanId \":0,\ " rateplanId \":0},\" roomNightExtra \":[]},{\" reservationRoomNightParams \":{\"日期\":\" 2016年6月6日\" \" totalPrice \":400,\" CURRENCYCODE \":\& #34; EUR \" \" crsRatePlanId \":0,\" rateplanId \":0},\" roomNightExtra \" :[]}]&#34 ;;

     

String reservationRoomNightBad =   " [{\" reservationRoomNightParams \":{\"日期\":\" 2016年6月5日\" \&# 34; totalPrice \":700,\" CURRENCYCODE \":\" EUR \" \" chRatePlanCodes \":{\& #34;日期\":\" 2016年6月5日\" \" totalPrice \":600,\" CURRENCYCODE \&#34 ;:\" EUR \" \" crsRatePlanId \":0,\" rateplanId \":0},\" crsRatePlanId \ ":12969,\" configuredCommission \":0,\" configuredCommissionType \":\" p \" \" rateplanId \":0},\" roomNightExtra \":[]}]&#34 ;;

我有这个类将字符串填充到:

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 hashmap,我没有问题。但是,当我留下该部分字符串时会出现问题。例如,尝试使用此字符串运行它(reservationRoomNightBad - hashmap)

  

String reservationRoomNightBad =   &#34; [{\&#34; reservationRoomNightParams \&#34;:{\&#34;日期\&#34;:\&#34; 2016年6月5日\&#34; \&# 34; totalPrice \&#34;:700,\&#34; CURRENCYCODE \&#34;:\&#34; EUR \&#34; \&#34; crsRatePlanId \&#34;:12969,\ &#34; configuredCommission \&#34;:0,\&#34; configuredCommissionType \&#34;:\&#34; p \&#34; \&#34; rateplanId \&#34;:0 },\&#34; roomNightExtra \&#34;:[]}]&#34 ;;

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您的代码:

private Map<String, String> reservationRoomNightParams;

只接受键字符号&amp;&amp; value String,但是您为此属性提供了一个对象:

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

使用以下方法解决此问题:

private Map<String, Object> reservationRoomNightParams;