Java 使用jsonpath访问json值?

Java 使用jsonpath访问json值?,java,jsonpath,Java,Jsonpath,我遵循使用jsonpath()的教程,并使用端点,试图解析符号TRXEUR的值LOT_SIZE,stepSize。返回的有效负载中包含的特定JSON片段包含在: { "symbol": "TRXEUR", "status": "TRADING", "baseAsset": "TRX", "baseAssetPrecision": 8, "quote

我遵循使用jsonpath()的教程,并使用端点,试图解析符号TRXEUR的值LOT_SIZE,stepSize。返回的有效负载中包含的特定JSON片段包含在:

{
"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"
]
}
更具体地说,如何从以下内容中提取
1.00000000

{
"filterType": "LOT_SIZE",
"minQty": "1.00000000",
"maxQty": "90000000.00000000",
"stepSize": "1.00000000"
}
{
"filterType": "LOT_SIZE",
"minQty": "1.00000000",
"maxQty": "90000000.00000000",
"stepSize": "1.00000000"
}
以下是我写的:

public class ParseJson {

    public static void main(String[] args) {

        try {
            URL url = new URL("https://api.binance.com/api/v3/exchangeInfo");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer content = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                content.append(inputLine);
            }
            in.close();

            final String jsonString = 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 (IOException e) {
            e.printStackTrace();
        }


    }
}
我可以访问符号
TRXEUR
并解析
symbols
,但如何从中提取
1.00000000

{
"filterType": "LOT_SIZE",
"minQty": "1.00000000",
"maxQty": "90000000.00000000",
"stepSize": "1.00000000"
}
{
"filterType": "LOT_SIZE",
"minQty": "1.00000000",
"maxQty": "90000000.00000000",
"stepSize": "1.00000000"
}

我可能不需要强制转换为返回类型(例如列表)上的对象,而是使用Map,但这样做有效:

import com.jayway.jsonpath.JsonPath;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

public class ParseJson {

    public static void main(String[] args) {

        try {
            URL url = new URL("https://api.binance.com/api/v3/exchangeInfo");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer content = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                content.append(inputLine);
            }
            in.close();

            final String jsonString = content.toString();
            List<Map<String, Object>> items = JsonPath.parse(jsonString).read("symbols");
            items.forEach(m -> {
                if (m.get("symbol").toString().toUpperCase().equalsIgnoreCase("BTCEUR")) {
                    List<Map<String, Object>> m3 = (List<Map<String, Object>>) m.get("filters");
                    m3.forEach(m2 -> {
                        if (m2.get("filterType").toString().equalsIgnoreCase("LOT_SIZE")) {
                            System.out.println("Stepsize " + m2.get("stepSize"));
                        }
                    });
                }
            });

        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}

你的原籍问题:

Object read = JsonPath.read(jsonString, "$.filters[?(@.filterType=='LOT_SIZE')].stepSize");
JSON from“https://api.binance.com/api/v3/exchangeInfo“:

如果您仍然想使用streams,那么第二个泛型是Object,因此我们确实需要显式地强制转换(列表):

String步长=项
.stream()
.filter(map->“BTCEUR”.equalsIgnoreCase(map.get(“symbol”).toString())
.map(map->(List)map.get(“过滤器”))
.findFirst()
.orelsetrow()
.stream()
.filter(map->“LOT\u SIZE”.equalsIgnoreCase(map.get(“filterType”).toString())
.findFirst()
.orelsetrow()
.get(“步长”)
.toString();
系统输出打印项次(步长);

删除了XPath标记,因为此问题似乎与XPath无关。错误消息
路径:$['symbol']]没有结果
似乎与我期望看到的不匹配-即路径:$['symbols']
没有结果。问题中的代码是否与堆栈跟踪对齐?JSON中没有名为
symbols
的项,而不是
List dataObject=JsonPath.parse(jsonString.read(“symbols”)
,使用
List items=JsonPath.parse(jsonString.read(“filters”)。然后您将有一个映射列表,您可以通过它进行迭代,这比试图操作字符串要容易得多。@andrewjames谢谢,我正在发布一个答案。@符号在rest有效负载响应中,我发布了一个响应片段。
Object read = JsonPath.read(jsonString, "$.filters[?(@.filterType=='LOT_SIZE')].stepSize");
Object read = JsonPath.read(jsonString, "$.symbols[?(@.symbol=='BTCEUR')].filters[?(@.filterType=='LOT_SIZE')].stepSize");
String stepSize = items
        .stream()
        .filter(map -> "BTCEUR".equalsIgnoreCase(map.get("symbol").toString()))
        .map(map -> (List<Map<String, Object>>) map.get("filters"))
        .findFirst()
        .orElseThrow()
        .stream()
        .filter(map -> "LOT_SIZE".equalsIgnoreCase(map.get("filterType").toString()))
        .findFirst()
        .orElseThrow()
        .get("stepSize")
        .toString();

System.out.println(stepSize);