I have a problem. I have the following 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"
}
]
}
In my java class I added the following code to parse the Json to the current class as object:
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;
}
}
The function should return a full filled Object, but I am struggling with the ArrayList<PriceDropSell>
. The problem is that I have no idea how to store the PriceDropSells
from my JSON as ArrayList<PriceDropSell>
. Every field gets stored in the class AgentStrategy, except the PriceDropSell. How can I parse that part to an ArrayList?
You don't need all the map stuff. Gson will parse directly into the object. But make sure the names match:
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);
}
}
Why not inspect what the actual object for "PriceDropSells" is? Just get that property without any casts and check with a debugger it's type:
Object priceDropSellsMap = map.get("PriceDropSells");
I suppose it should be an array or a List.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.