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

我使用 jsonpath 来选择 accountNo 如果 mode="BANK" 或者如果 mode=WALLET 那么 walletId 应该被选择。我尝试了表达式 $.customer.modeDetails[$.customer.mode=='BANK'].accountNo 但它不起作用。请帮助我。

4

1 回答 1

1
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"]
于 2020-11-11T14:12:10.393 回答