我有价值的字符串折扣:
"[{\"startInterval\":0,\"endInterval\":3,\"discount\":1000.0},
{\"startInterval\":3,\"endInterval\":6,\"discount\":750.0},
{\"startInterval\":6,\"endInterval\":9,\"discount\":500.0},
{\"startInterval\":9,\"endInterval\":12,\"discount\":10.0}]"
我的折扣类有型号:
public class Discount {
private int startInterval;
private int endInterval;
private double discount;
public Discount() {
}
public int getStartInterval() {
return startInterval;
}
public void setStartInterval(int startInterval) {
this.startInterval = startInterval;
}
public int getEndInterval() {
return endInterval;
}
public void setEndInterval(int endInterval) {
this.endInterval = endInterval;
}
public Double getDiscount() {
return discount;
}
public void setDiscount(Double discount) {
this.discount = discount;
}
}
我想将字符串序列化为List<Discount>
。我怎么做?我已经尝试将字符串转换为JSONArray
(gson和json),但我收到了一些错误。
请注意:
[{"startInterval":0,"endInterval":3,"discount":1000.0},
{"startInterval":3,"endInterval":6,"discount":750.0},
{"startInterval":6,"endInterval":9,"discount":500.0},
{"startInterval":9,"endInterval":12,"discount":10.0}]
存储在mysql db中。我将其提取为字符串折扣,然后尝试序列化。
答案 0 :(得分:1)
使用GSON:
String json = /* your json */
Type type = new TypeToken<List<Discount>>().getType();
List<Discount> discounts = new Gson().fromJson(json , type);