将Map <string,string =“”>转换为POJO列表 - java.lang.IllegalStateException:预期BEGIN_ARRAY但是STRING

时间:2018-02-26 10:42:33

标签: java gson

目前,我计划将以下Map<String, String>转换为POJO的List

Map<String, String> m = new HashMap<>();
m.put("stock_price_alerts", "[{\"code\":\"KO\",\"rise_above\":7.0,\"size\":1000.89,\"price\":10,\"time\":1519625135173,\"fall_below\":null},{\"code\":\"BRK-B\",\"rise_above\":180,\"size\":100,\"price\":190,\"time\":1519160399911,\"fall_below\":null}]");
System.out.println(m);

当我们在控制台打印Map时,它看起来像

{stock_price_alerts=[{"code":"KO","rise_above":7.0,"size":1000.89,"price":10,"time":1519625135173,"fall_below":null},{"code":"BRK-B","rise_above":180,"size":100,"price":190,"time":1519160399911,"fall_below":null}]}

我准备了2个POJO课程。

StockPriceAlert.java

package sandbox;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class StockPriceAlert {

    @SerializedName("code")
    @Expose
    public String code;
    @SerializedName("fall_below")
    @Expose
    public Object fallBelow;
    @SerializedName("rise_above")
    @Expose
    public long riseAbove;
    @SerializedName("price")
    @Expose
    public long price;
    @SerializedName("time")
    @Expose
    public long time;
    @SerializedName("size")
    @Expose
    public long size;

    /**
    * No args constructor for use in serialization
    * 
    */
    public StockPriceAlert() {
    }

    /**
    * 
    * @param fallBelow
    * @param time
    * @param price
    * @param riseAbove
    * @param code
    * @param size
    */
    public StockPriceAlert(String code, Object fallBelow, long riseAbove, long price, long time, long size) {
        super();
        this.code = code;
        this.fallBelow = fallBelow;
        this.riseAbove = riseAbove;
        this.price = price;
        this.time = time;
        this.size = size;
    }

}

StockPriceAlertResponse.java

package sandbox;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class StockPriceAlertResponse {

    @SerializedName("stock_price_alerts")
    @Expose
    private List<StockPriceAlert> stockPriceAlerts = null;

    /**
     * No args constructor for use in serialization
     *
     */
    public StockPriceAlertResponse() {
    }

    /**
     *
     * @param stockPriceAlerts
     */
    public StockPriceAlertResponse(List<StockPriceAlert> stockPriceAlerts) {
        super();
        this.stockPriceAlerts = stockPriceAlerts;
    }

    public List<StockPriceAlert> getStockPriceAlerts() {
        return stockPriceAlerts;
    }

    public void setStockPriceAlerts(List<StockPriceAlert> stockPriceAlerts) {
        this.stockPriceAlerts = stockPriceAlerts;
    }

}

我编写了以下代码来执行转换。

package sandbox;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author yccheok
 */
public class Main {  
    public static void main(String[] args) {
        Map<String, String> m = new HashMap<>();
        m.put("stock_price_alerts", "[{\"code\":\"KO\",\"rise_above\":7.0,\"size\":1000.89,\"price\":10,\"time\":1519625135173,\"fall_below\":null},{\"code\":\"BRK-B\",\"rise_above\":180,\"size\":100,\"price\":190,\"time\":1519160399911,\"fall_below\":null}]");

        System.out.println(m);

        final Gson gson = new GsonBuilder().create();

        JsonElement jsonElement = gson.toJsonTree(m);
        StockPriceAlertResponse stockPriceAlertResponse = gson.fromJson(jsonElement, StockPriceAlertResponse.class);
        List<StockPriceAlert> stockPriceAlerts = stockPriceAlertResponse.getStockPriceAlerts();
    }
}

但是,我得到以下例外

  

线程中的异常&#34; main&#34; com.google.gson.JsonSyntaxException:   java.lang.IllegalStateException:预期为BEGIN_ARRAY但是为STRING

知道如何解决这个问题吗?

注意,Map<String, String> m从第三方库输出。所以,那个输入是我无法控制的。

0 个答案:

没有答案