放心-如何在“ JSONObject主体”中传递对象

时间:2018-09-06 16:43:42

标签: rest-assured

执行后的以下代码

    $("#cres_fontup").click(function(){
        $( document.body ).trigger({
        type: 'keypress',
        which: 61,
        keyCode: 61
        });
    });  

返回

public void RegistrationSuccessful()
{       
    RestAssured.baseURI ="http://restapi.demoqa.com/customer";
    RequestSpecification request = RestAssured.given();

    JSONObject requestParams = new JSONObject();
    requestParams.put("FirstName", "Virender"); // Cast
    requestParams.put("LastName", "Singh");
    request.body(requestParams.toJSONString());
    Response response = request.post("/register");
}

有人可以指导下面的JSON放心使用的代码吗?

{
    "FirstName": "Virender",
    "LastName": "Singh"
}

2 个答案:

答案 0 :(得分:0)

您可以为此使用JSONObject,HashMap或POJO

使用 JSONObject 的示例代码,我尚未测试下面的代码,所以请告诉我它是否无效

SELECT coloumn1,column2 FROM `myTable` WHERE `status`=1 AND `group`=6 ORDER BY RAND() LIMIT 0, :max

也是使用 HashMap

的示例
JSONObject requestparams = new JSONObject();
JSONArray authArray = new JSONArray();
JSONObject authparam = new JSONObject();

    requestParams.put("FirstName", "Virender");
    requestParams.put("LastName", "Singh");

        authparam.put("Line1", "Flat no 101");
        authparam.put("Area", "Andheri");
        authparam.put("City", "Mumbai");
        authArray.add(authparam);

        requestparams.put("Address", authparam);

    req.body(requestparams.toJSONString());
    Response response = req.post("http://restapi.demoqa.com/customer/register");

答案 1 :(得分:0)

处理嵌套json的实际方法是通过POJO序列化json。给定json的POJO为:[这里我正在使用gson]

public class Address {

@SerializedName("Line1")
@Expose
private String line1;
@SerializedName("area")
@Expose
private String area;
@SerializedName("City")
@Expose
private String city;

public String getLine1() {
return line1;
}

public void setLine1(String line1) {
this.line1 = line1;
}

public String getArea() {
return area;
}

public void setArea(String area) {
this.area = area;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

}
-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

@SerializedName("FirstName")
@Expose
private String firstName;
@SerializedName("LastName")
@Expose
private String lastName;
@SerializedName("Address")
@Expose
private Address address;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public Address getAddress() {
return address;
}

public void setAddress(Address address) {
this.address = address;
}

}

现在,当您要创建有效负载时,只需构造Example类的对象并将其传递给测试方法即可。

Example  example = new Example(
        "Virender", "Singh", new Address("Line1", "flat no 101", "andheri", "Mumbai")
        );

public void RegistrationSuccessful(Example example){
    // Method definition
    RequestSpecification request = RestAssured.given();
    request.body(example);
    Response _response = _request.post("//EndPoint");
}

通过这种方式,您可以处理更复杂的有效负载。而且,所有POJO都可以从json生成。