Accessing json value using jsonpath?
问题
https://stackoverflow.com/questions/67451735/accessing-json-value-using-jsonpath
I'm following the tutorial for using jsonpath (https://www.baeldung.com/guide-to-jayway-jsonpath) and using the endpointhttps://api.binance.com/api/v3/exchangeInfoI'm attempting to parse the value LOT_SIZE, stepSize for the symbol TRXEUR. The specific piece of JSON contained in the returned payload is contained in:
{
"symbol":"TRXEUR",
"status":"TRADING",
"baseAsset":"TRX",
"baseAssetPrecision":8,
"quoteAsset":"EUR",
"quotePrecision":8,
"quoteAssetPrecision":8,
"baseCommissionPrecision":8,
"quoteCommissionPrecision":8,
"orderTypes":[
"LIMIT",
"LIMIT_MAKER",
"MARKET",
"STOP_LOSS_LIMIT",
"TAKE_PROFIT_LIMIT"
],
"icebergAllowed":true,
"ocoAllowed":true,
"quoteOrderQtyMarketAllowed":true,
"isSpotTradingAllowed":true,
"isMarginTradingAllowed":false,
"filters":[
{
"filterType":"PRICE_FILTER",
"minPrice":"0.00010000",
"maxPrice":"1000.00000000",
"tickSize":"0.00010000"
},
{
"filterType":"PERCENT_PRICE",
"multiplierUp":"5",
"multiplierDown":"0.2",
"avgPriceMins":5
},
{
"filterType":"LOT_SIZE",
"minQty":"1.00000000",
"maxQty":"90000000.00000000",
"stepSize":"1.00000000"
},
{
"filterType":"MIN_NOTIONAL",
"minNotional":"10.00000000",
"applyToMarket":true,
"avgPriceMins":5
},
{
"filterType":"ICEBERG_PARTS",
"limit":10
},
{
"filterType":"MARKET_LOT_SIZE",
"minQty":"0.00000000",
"maxQty":"904859.10069444",
"stepSize":"0.00000000"
},
{
"filterType":"MAX_NUM_ORDERS",
"maxNumOrders":200
},
{
"filterType":"MAX_NUM_ALGO_ORDERS",
"maxNumAlgoOrders":5
}
],
"permissions":[
"SPOT"
]
}
More specifically how to extract1.00000000
from :
{
"filterType":"LOT_SIZE",
"minQty":"1.00000000",
"maxQty":"90000000.00000000",
"stepSize":"1.00000000"
}
Here is what I've written :
publicclassParseJson{
publicstaticvoidmain(String[]args){
try{
URLurl=newURL("https://api.binance.com/api/v3/exchangeInfo");
HttpURLConnectioncon=(HttpURLConnection)url.openConnection();
con.setRequestMethod("GET");
BufferedReaderin=newBufferedReader(newInputStreamReader(con.getInputStream()));
StringinputLine;
StringBuffercontent=newStringBuffer();
while((inputLine=in.readLine())!=null){
content.append(inputLine);
}
in.close();
finalStringjsonString=content.toString();
List<Object>dataObject=JsonPath.parse(jsonString).read("symbols");
dataObject.forEach(x->{
if(x.toString().toUpperCase().contains("TRXEUR")){
List<Object>lo=JsonPath.parse(x.toString()).read("symbol");
}
}
);
}catch(IOExceptione){
e.printStackTrace();
}
}
}
Which returns :
20:52:10.428[main]DEBUGcom.jayway.jsonpath.internal.path.CompiledPath-Evaluatingpath:$['symbols']
20:52:10.469[main]DEBUGcom.jayway.jsonpath.internal.path.CompiledPath-Evaluatingpath:$['symbol']
Exceptioninthread"main"com.jayway.jsonpath.PathNotFoundException:Noresultsforpath:$['symbol']
atcom.jayway.jsonpath.internal.path.EvaluationContextImpl.getValue(EvaluationContextImpl.java:133)
atcom.jayway.jsonpath.JsonPath.read(JsonPath.java:187)
atcom.jayway.jsonpath.internal.JsonContext.read(JsonContext.java:102)
atcom.jayway.jsonpath.internal.JsonContext.read(JsonContext.java:89)
atcom.reactive.api.scenarios.ParseJson.lambda$main$0(ParseJson.java:37)
atjava.base/java.util.ArrayList.forEach(ArrayList.java:1540)
atcom.reactive.api.scenarios.ParseJson.main(ParseJson.java:34)
Processfinishedwithexitcode1
I can access the symbolTRXEUR
andsymbols
is parsed but how to extract1.00000000
from :
{
"filterType":"LOT_SIZE",
"minQty":"1.00000000",
"maxQty":"90000000.00000000",
"stepSize":"1.00000000"
}
?
解答
从多层Json 中找到满足 symbols 的值为 TRXEUR,filterType 的值为 LOT_SIZE 的 stepSize 的值。用 Java 实现代码较长。
用 Java 下的开源包 SPL 很容易写,只要 1 句:
A |
|
1 |
=json(httpfile("https://api.binance.com/api/v3/exchangeInfo").read()).symbols.select(symbol=="TRXEUR").filters.select(~.filterType=="LOT_SIZE").stepSize |
SPL 提供了 JDBC 供 Java 调用,把上面的脚本存为 getvalue.splx,在 Java 中以存储过程的方式调用脚本文件:
…
Class.forName("com.esproc.jdbc.InternalDriver");
con= DriverManager.getConnection("jdbc:esproc:local://");
st = con.prepareCall("call getvalue()");
st.execute();
…
English version