java - 如何使用简单的 json 在请求正文下传递 json 数组

标签 java json rest-assured

我正在使用下面的代码来自动化 REST API。 请帮助我理解如何将整个 json 数据放入下面提到的示例数据中,因为输入有数组,而到目前为止我使用的是没有数组的平面 json

Method Dummy()
{
    RestAssured.baseURI ="http://mydummyURL";
    RequestSpecification request = RestAssured.given();
    JSONObject requestParams = new JSONObject();
    requestParams.put("id", "THAILAND"); //Issue is with this code 
    request.header("Content-Type", "application/json");
    request.body(requestParams.toJSONString());
    Response response = request.post("/EndPoint");
}

json 正文如下所示

{
    "tag1": "value1",
    "tag2": "value2",
    "tag3": {
        "tag31": "value31",
        "tag32": "value32"
    },
    "tag4": [{
            "domainName": "ABC",
            "domainId": "123ABC123",
            "domainGUID": "TestMyDomain"
        },
        {
            "domainName": "XYZ",
            "domainId": "123XYZ123",
            "domainGUID": "TestMyDomain"
        }
    ]
}

最佳答案

ArrayList<JSONObject> array= new ArrayList<JSONObject>();
    JSONObject json= new JSONObject();

    try {
        json.put("key", "value");// your json
    } catch (JSONException e) {
        e.printStackTrace();
    }
    array.add(json);
    String printjsonarray= array.toString();// pass this into the request 

关于java - 如何使用简单的 json 在请求正文下传递 json 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55631016/

相关文章:

java - 检测java中要返回哪个类

java - 无法在 JPanel 上绘图

java - 使用 ManyToMany 进行 JPQL 查询

javascript - 如何使用可能包含空单元格的 JSON 数据填充表格 (AngularJS)

maven - 无法访问在 Eclipse 和 Rest Assured 中找不到的 org.hamcrest.Matcher 的 org.hamcrest.Matcher 类文件

java - 无法获得放心的输出值

java - 有没有可能直接在jvm中实现java应用的一部分?

javascript - 如何将文件对象添加到JavaScript对象中?

json - 使用 InputReader 解析包含数组的 JSON 字符串?

java - RestAssured : annot determine how to serialize content-type application/x-www-form-urlencoded. 如何创建具有键/值结构的请求?