我有一个问题。我有以下JSON:
{
"Market":"USDT",
"Coin":"BTC",
"Period":"1h",
"EmergencyPerc":-25,
"TakeProfitPerc":1.2,
"ProtectiveOrdersEnabled":"no",
"EMACrossMarginPerc":0.08,
"EMABuySellPeriod":"5m",
"EMABuySellNameLow":"EMA5",
"EMABuySellNameHigh":"EMA20",
"EMAUnfreezePeriod":"1h",
"EMAUnfreezeNameLow":"EMA20",
"EMAUnfreezeNameHigh":"EMA200",
"SimTemplate":"t001",
"PriceDropSells":[
{
"Period":"3h",
"Number":"4",
"Perc":"-1.0"
},
{
"Period":"1h",
"Number":"2",
"Perc":"-2.0"
}
]
}
在我的java类中,我添加了以下代码来将Json解析为当前类的对象:
public class AgentStrategy {
private String market;
private String coin;
private String period;
private double emergencyPerc;
private double takeProfitPerc;
private String protectiveOrdersEnabled;
private double EMACrossMarginPerc;
private String EMABuySellPeriod;
private String EMABuySellNameLow;
private String EMABuySellNameHigh;
private String EMAUnfreezePeriod;
private String EMAUnfreezeNameLow;
private String EMAUnfreezeNameHigh;
private String simTemplate;
private ArrayList<AgentStrategyPriceDropSells> priceDropSells = new ArrayList<>();
public AgentStrategy parseJsonToObject(String jsonString) {
Gson gson = new Gson();
gson.fromJson(jsonString, AgentStrategy.class);
Map map = gson.fromJson(jsonString, Map.class);
this.market = map.get("Market").toString();
this.coin = map.get("Coin").toString();
this.period = map.get("Period").toString();
this.emergencyPerc = Double.parseDouble(map.get("EmergencyPerc").toString());
this.takeProfitPerc = Double.parseDouble(map.get("TakeProfitPerc").toString());
this.protectiveOrdersEnabled = map.get("ProtectiveOrdersEnabled").toString();
this.EMACrossMarginPerc = Double.parseDouble(map.get("EMACrossMarginPerc").toString());
this.EMABuySellPeriod = map.get("EMABuySellPeriod").toString();
this.EMABuySellNameLow = map.get("EMABuySellNameLow").toString();
this.EMABuySellNameHigh = map.get("EMABuySellNameHigh").toString();
this.EMAUnfreezePeriod = map.get("EMAUnfreezePeriod").toString();
this.EMAUnfreezeNameLow = map.get("EMAUnfreezeNameLow").toString();
this.EMAUnfreezeNameHigh = map.get("EMAUnfreezeNameHigh").toString();
this.simTemplate = map.get("SimTemplate").toString();
// PRICE DROP SELLS
Map priceDropSellsMap = (Map) map.get("PriceDropSells");
// ???
return this;
}
private class PriceDropSell {
private String period;
private String number;
private double perc;
}
}
函数应该返回一个完整的填充对象,但我正在与ArrayList<PriceDropSell>
作斗争。问题是,我不知道如何存储PriceDropSells
从我的JSON作为ArrayList<PriceDropSell>
。除了PriceDropSell之外,每个字段都存储在类AgentStrategy中。我怎么把这部分解析成数组列表呢?
您不需要所有的映射内容。Gson将直接解析为对象。但是要确保名称匹配:
class Test
{
private class PriceDropSell {
private String Period;
private String Number;
private double Perc;
}
public class AgentStrategy {
private String Market;
private String Coin;
private String Period;
private double EmergencyPerc;
private double TakeProfitPerc;
private String ProtectiveOrdersEnabled;
private double EMACrossMarginPerc;
private String EMABuySellPeriod;
private String EMABuySellNameLow;
private String EMABuySellNameHigh;
private String EMAUnfreezePeriod;
private String EMAUnfreezeNameLow;
private String EMAUnfreezeNameHigh;
private String SimTemplate;
private ArrayList<PriceDropSell> PriceDropSells;
}
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader bufferedReader = new BufferedReader(new FileReader(path_to_json_file));
Gson gson = new Gson();
AgentStrategy agent = gson.fromJson(bufferedReader, AgentStrategy.class);
System.out.println(agent.PriceDropSells.get(0).Perc);
}
}
为什么不检查实际对象为" pricedropsell ";是什么?只需要获得那个属性,不需要任何强制转换,然后用调试器检查它的类型:
对象priceDropSellsMap = map.get(" pricedropsell ";
我想它应该是一个数组或列表。