Как передать массив JSON в теле запроса, используя просто JSON - PullRequest
1 голос
/ 11 апреля 2019

Я использую приведенный ниже код для автоматизации REST API.Пожалуйста, помогите мне понять, как я могу поместить целые данные json для выборочных данных, упомянутых ниже, поскольку на входе есть массивы, тогда как до сих пор я использовал плоские jsons без массивов

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"
        }
    ]
}

Ответы [ 2 ]

0 голосов
/ 11 апреля 2019

Если вы хотите использовать JSON В Java , попробуйте JSONArray:

//Create your json payload object
JSONObject jsonObject = new JSONObject();
jsonObject.put("tag1", "value1");

//Create your domain objects 
JSONObject domainObj = new JSONObject();
domainObj.put("domainName", "ABC");
domainObj.put("domainId", "123ABC123");
domainObj.put("domainGUID", "TestMyDomain");

//Add created domain objects to json array
JSONArray domains = new JSONArray();
domains.put(domainObj);

//Add array to json payload object
jsonObject.put("tag4", domains);

RestAssured.given()
    .contentType(ContentType.JSON)
    .body(jsonObject.toString())
    .when()
    .post("http://www.example.com");

Вот Javadoc, чтобы посмотреть на https://stleary.github.io/JSON-java/

0 голосов
/ 11 апреля 2019
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 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...