简体   繁体   English

使用放心的方式解析Json响应

[英]Parse through Json response using rest-assured

I want to parse through my JSON response to validate the response i obtained. 我想解析我的JSON响应以验证我获得的响应。

 [
    {
        "Emp": "00000",
        "Emp_ID": 901,
        "First_Name": "agar",
        "Last_Name": "vedi",
        "Country": "India",
        "EmpLocation": "Noida"
    },
    {
        "Emp": "001",
        "Emp_ID": 383,
        "First_Name": "Manoj",
        "Last_Name": "jee"
        "Country": "India",
        "EmpLocation": "Noida"
    }
]

Now , I am using Rest-assured java API for this , I went through the tutorial on toolsQA and their they are using 现在,我为此使用了Rest-assured Java API,我通过了关于toolsQA的教程,他们正在使用

Response response = httpRequest.request(Method.GET, "/Hyderabad");

For the Json:- 对于Json:-

{
    “City”: “Hyderabad”,
    “Temperature”: “31.49 Degree celsius”,
    “Humidity”: “62 Percent”,
    “Weather Description”: “scattered clouds”,
    “Wind Speed”: “3.6 Km per hour”,
    “Wind Direction degree”: “270 Degree”
   }

Now this response is a single JSON object. 现在,此响应是单个JSON对象。 But mine is nested in a JSON Array . 但是我的嵌套在JSON Array中。

How do I parse through such nested Json objects and arrays ? 如何解析此类嵌套的Json对象和数组? as Json response can come in all combinations of Array and Objects . 因为Json响应可以出现在Array和Objects的所有组合中。

Is their a method in rest-assured which provides value corresponding to the key? 他们是否放心地提供了对应于钥匙的价值? example: "key":"value" I go to the key and obtain the value through that method? 例如:“ key”:“ value”我去找钥匙,并通过该方法获得价值?

Thanks. 谢谢。

If you have mapped the JSON to PoJo, you just need to change the request to: 如果您已将JSON映射到PoJo,则只需将请求更改为:

City[] cities = given()
                    .when()
                    .get("/Hyderabad")
                    .then()
                    .response()
                    .getBody()
                    .as(City[].class);

This is how RestAssured can deserialize an array of objects. 这就是RestAssured可以反序列化对象数组的方式。 You need to map it to Array of Objects. 您需要将其映射到对象数组。

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

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