【问题标题】:GSON.fromJson() issue with hashmap哈希图的 GSON.fromJson() 问题
【发布时间】:2016-01-14 16:48:05
【问题描述】:

我正在使用 GSON 2.5。

我有 2 个字符串:

String reservationRoomNightGood = "[{\"reservationRoomNightParams\":{\"date\":\"2016-06-05\",\"totalPrice\":600,\"currencyCode\":\"EUR\",\"crsRatePlanId\ ":0,\"rateplanId\":0},\"roomNightExtra\":[]},{\"reservationRoomNightParams\":{\"date\":\"2016-06-06\",\"totalPrice \":400,\"currencyCode\":\"EUR\",\"crsRatePlanId\":0,\"rateplanId\":0},\"roomNightExtra\":[]}]";

String reservationRoomNightBad = "[{\"reservationRoomNightParams\":{\"date\":\"2016-06-05\",\"totalPrice\":700,\"currencyCode\":\"EUR\",\"chRatePlanCodes\ ":{\"date\":\"2016-06-05\",\"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)

String reservationRoomNightBad = "[{\"reservationRoomNightParams\":{\"date\":\"2016-06-05\",\"totalPrice\":700,\"currencyCode\":\"EUR\",\"crsRatePlanId\ ":12969,\"configuredCommission\":0,\"configuredCommissionType\":\"P\",\"rateplanId\":0},\"roomNightExtra\":[]}]";

有什么想法吗?

【问题讨论】:

    标签: java hashmap gson


    【解决方案1】:

    你的代码:

    private Map<String, String> reservationRoomNightParams;
    

    只接受key String && value String,但是你给了这个属性一个对象:

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

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

    private Map<String, Object> reservationRoomNightParams;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-21
      • 2018-01-16
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多