【问题标题】:JsonPath selecting object field if multiple condition matches如果多个条件匹配,则 JsonPath 选择对象字段
【发布时间】:2020-11-11 10:25:20
【问题描述】:
{
    "userName": "test"
    "customer": {
        "mode": "BANK",
        "modeDetails": {
            "accountNo": "12345678901001",
            "walletId": "11324354@paypal"
        }
    }
}

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

【问题讨论】:

    标签: java json jsonpath json-path-expression


    【解决方案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"]
    

    【讨论】:

      猜你喜欢
      • 2014-09-21
      • 1970-01-01
      • 2020-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多