如果多个条件匹配,则JsonPath选择对象字段

问题描述

{
    "userName": "test"
    "customer": {
        "mode": "BANK","modeDetails": {
            "accountNo": "12345678901001","walletId": "11324354@paypal"
        }
    }
}

如果使用mode =“ BANK”,否则我使用jsonpath选择accountNo,否则如果选择mode = WALLET,则应该选择walletId。我尝试了表达式$ .customer.modeDetails [$。customer.mode =='BANK']。accountNo,但是它不起作用。请帮助我。

解决方法

public static void main(String[] args)  {   
    String jsonString = "{\r\n" + 
            "    \"userName\": \"test\",\r\n" + 
            "    \"customer\": {\r\n" + 
            "        \"mode\": \"BANK\",\r\n" + 
            "        \"modeDetails\": {\r\n" + 
            "            \"accountNo\": \"12345678901001\",\r\n" + 
            "            \"walletId\": \"11324354@paypal\"\r\n" + 
            "        }\r\n" + 
            "    }\r\n" + 
            "}";
    DocumentContext docCtx = JsonPath.parse(jsonString);
    JsonPath jsonPath = JsonPath.compile("$..[?(@.customer.mode==\"BANK\")].customer.modeDetails.accountNo");
    List<String> accounts = docCtx.read(jsonPath);
    System.out.println(accounts);
}

结果

["12345678901001"]