在具有变量的JSON响应中提取值
我试图从JSON文件中返回声明变量的ID:我希望我的Java代码在JSON响应中搜索以前的变量,并且一旦找到它 - >返回相应的ID。 我正在使用Java重新安排的。
JSON文件响应示例:
[
{
"class" : "test 1",
"id" : 5,
"variable" : 87
},
{
"class" : "test 2",
"id" : 7,
"variable" : 76
}
]
现在,我做类似的事情,但我不知道:
Response response = given()
.config(config)
.header("accept","application")
.when()
.get("requestID")
.then()
.extract().response();
String id = String.valueOf(response.
body().
jsonPath().
get("id").toString().contains(variable));
谢谢
I am trying to return the id of a declared variable from a json file: i want my java code to search for the previous variable in the json response and once it is found -> return the corresponding id.
I am using RestAssured in java.
Json file response example:
[
{
"class" : "test 1",
"id" : 5,
"variable" : 87
},
{
"class" : "test 2",
"id" : 7,
"variable" : 76
}
]
Now, i do something like that but i have no idea:
Response response = given()
.config(config)
.header("accept","application")
.when()
.get("requestID")
.then()
.extract().response();
String id = String.valueOf(response.
body().
jsonPath().
get("id").toString().contains(variable));
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这里快速解决方案,按照GPATH语法查询。
Quick solution here, query by condition with GPath syntax.
您需要的只是解析您的JSON。这可以非常简单。您可以将给定的JSON解析为
list< map>
,也可以创建一个简单的类,让我们称其为myClassInfo
,然后将您的JSON解析为list< myclassinfo>
。您的myClassInfo
看起来像这样:现在您可以使用JSON Jackson库轻松解析它:
Formaps:
并且对于
myClassInfo
类,现在您可以浏览列表,然后从每个
MAP
或myClassInfo
中提取所需ID。为了简化一些简化的内容,您可以使用我写的Mgntutils库中的
jsonutils
类解析。这是带有jsonutils
类的代码(仅适用于myClassInfo
class:请注意,在这种情况下,您不必实例化和配置
objectmapper
实例由于ReadObjectFromjSonstring
是静态方法。 /mgntutils“ rel =“ nofollow noreferrer”>此处 和源代码和javadoc的库本身在github上此处。 rel =“ nofollow noreferrer”>在这里All you need is just to parse your Json. This could be done very simply. You can parse your given Json to a
List<Map>
or you can create a simple class lets call itMyClassInfo
and parse your Json to aList<MyClassInfo>
. YourMyClassInfo
would look like this:Now you can easily parse it using Json Jackson library:
ForMaps:
And for
MyClassInfo
classNow you can just go through your list and extract from each
map
orMyClassInfo
the required id.Just to simplify it a bit more you can do parsing with
JsonUtils
class from MgntUtils library written by me. Here is the code withJsonUtils
class (just forMyClassInfo
class:Note that in this case you won't have to instantiate and configure
ObjectMapper
instance asreadObjectFromJsonString
is a static method. Anyway if you are interested in using my library you can find maven artifacts here and The library itself with source code and javadoc is on Github here. Javadoc forJsonUtils
class is here我们可以使用JSONPATH获取匹配我们标准的对象列表并返回列表或地图。
在这里,我假设变量为
class ='test 1'
。您可以给出任何标准以与之匹配。请参阅: https://github.com/json-path/jsonpath/jsonpath
We can use jsonpath to get a list of objects matching our criteria and return a list or map.
Here I'm assuming the variable is
class='test 1'
. You can give any criteria to match against.Refer: https://github.com/json-path/JsonPath