从graphql查询中获取键值

k5hmc34c  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(257)

我需要从graphql查询中获取值并用字符串验证它,我尝试将其转换为json并使用jsonpath获取值,但是失败了
这是请求查询

{
  ethereum {
    dexTrades(date: {is:"2020-11-29"} options: {limit: 10}
      baseCurrency: {is: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"}
        quoteCurrency:{is: "0xdac17f958d2ee523a2206206994597c13d831ec7"}
    ){

      baseCurrency {
        symbol
        address
      }
      baseAmount

      quoteCurrency {
        symbol
        address
      }
      quoteAmount

      quotePrice

    }
  }
}

我想我得到的日期和以下是我正在尝试的代码

public String getnodefromjson(String payload,String path) throws JSONException {
//        System.out.println("test"+payload.toString());
        String symbol=null;
        if(isJSONValid(payload)){

            symbol=""+JsonPath.from(payload).getString(path);

            try {
                System.out.println("symbol value 9s - " + symbol.toString());
            }catch (NullPointerException ex){
                System.out.println("Key doesnt exist - "+path);
            }
        }else {
            printResults("Not a valid json file");

        }

调用方法

getnodefromjson(jsoncontent,"$..date");

下面是错误消息

java.lang.IllegalArgumentException: The parameter "date" was used but not defined. Define parameters using the JsonPath.params(...) function
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:72)
    at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:105)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:59)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:263)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:286)
    at io.restassured.internal.path.json.JSONAssertion.getAsJsonObject(JSONAssertion.groovy:50)
    at io.restassured.internal.path.json.JSONAssertion$getAsJsonObject.callCurrent(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:51)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:171)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:185)
    at io.restassured.internal.path.json.JSONAssertion.getResult(JSONAssertion.groovy:28)
    at io.restassured.path.json.JsonPath.get(JsonPath.java:203)
    at io.restassured.path.json.JsonPath.getString(JsonPath.java:352)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题