简体   繁体   English

如何使用 RestAssured.JsonPath 从 JSON 获取特定属性的值

[英]How to get the value of particular attribute from a JSON using RestAssured.JsonPath

I'm trying to hit an API thru RestAssured + Java code and able to get some response as you can see in this post.我正在尝试通过 RestAssured + Java 代码访问 API 并能够得到一些响应,正如您在这篇文章中看到的那样。 But I need to get the value of particular node / attribute ie errorParams which is present in the JSON and print in the Java Console.但我需要获取 JSON 中存在的特定节点/属性的值,即errorParams并在 Java 控制台中打印。

{
   "customerId":null,
   "errorDetails":[
      [
         {
            "errorCode":"ABC_2021",
            "errorParams":"Input Customer Id"
         }
      ]
   ]
}

I tried like this and not working.我试过这样但不工作。

JsonPath jsp = new JsonPath(response.getBody().asString());

System.out.println(jsp.getString("errorDetails.errorParams"));

System.out.println(jsp.getString("$['errorDetails']['errorParams']"));

Any suggestions or working script would be helpful for me.任何建议或工作脚本都会对我有所帮助。

Thanks,谢谢,
Karunagara Pandi G卡鲁纳加拉潘迪 G

That one works just fine:那个工作得很好:

System.out.println(jsp.getString("errorDetails.errorParams"));

So I assumed you retrieving your JsonPath in the wrong way, try:所以我假设您以错误的方式检索您的 JsonPath,请尝试:

JsonPath jsp = response.jsonPath();

or或者

JsonPath jsp = new JsonPath(response.asString());

you can use the following snippet it works for me您可以使用以下对我有用的代码段

System.out.println(response.jsonPath().getString("errorDetails.errorParams"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM