Currently, I plan to convert the following Map<String, String> to List of POJO.
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);
When we print out the Map at console, it looks like
{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}]}
I prepare 2 POJO classes.
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;
}
}
I wrote the following code to perform conversion.
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();
}
}
However, I get the following Exception
Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING
Any idea how I can resolve this?
Note, Map<String, String> m is output from 3rd party library. So, that input is out of my control.