如何仅使用json在请求正文下传递json数组

时间:2019-04-11 11:05:31

标签: java json rest-assured

我正在使用以下代码自动执行REST API。 请帮助我了解如何将整个json数据用作下面提到的示例数据,因为输入具有数组,而到目前为止,我使用的是不带数组的平面json

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

json主体如下所示

        public Dictionary<string, object> GetPendingEmployeeItem(int itemId)
        {
            var list = _app.Sharepoint.Web.Lists.GetByTitle("New Employee");
            var query = new CamlQuery();
            query.ViewXml = "<View></View>";

            var collection = list.GetItems(query);
            _app.Sharepoint.Load(collection, items => items.Include(
                item => item.Id,
                item => item.DisplayName,
                item => item.FieldValuesAsText));
            _app.Sharepoint.ExecuteQuery();

            return ConvertToDictionary(collection.GetById(itemId));
        }

        private Dictionary<string, object> ConvertToDictionary(ListItem item)
        {
            var dic = new Dictionary<string, object>();
            foreach (var pair in item.FieldValuesAsText.FieldValues)
            {
                dic.Add(pair.Key, pair.Value);
            }

            return dic;
        }

3 个答案:

答案 0 :(得分:0)

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 

答案 1 :(得分:0)

如果要使用Java中的 JSON ,请尝试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/

答案 2 :(得分:0)

    ObjectMapper mapper = new ObjectMapper();

//为数组内的变量创建一个Java类。

JsonArrayData tag4paramVal1 = new JsonArrayData("ABC","123ABC123","TestMyDomain");
JsonArrayData tag4paramVal2 = new JsonArrayData("XYZ","123XYZ123","TestMyDomain");

    Object[] tag4ValArray = {tag4paramVal1,tag4paramVal2};

    String reqJson = null;
    List<String> tag4Data = new ArrayList<String>();

    for(Object obj:tag4ValArray){

        reqJson = mapper.writeValueAsString(obj);
        System.out.println(reqJson);
        tag4Data.add(reqJson);

    }

    System.out.println(tag4Data);

    HashMap<String,List<String>> finalReq = new HashMap<String,List<String>>();
    finalReq.put("\"tag4\":",tag4Data);


    String finalreqString = finalReq.toString();
    System.out.println(finalreqString);

    finalreqString = finalreqString.replace('=', ' ');
    System.out.println(finalreqString);

///使用上面的String作为POST请求的参数。您将获得所需的JSON数组。

// JsonArrayData类代码

公共类JsonArrayData {

String domainName;
String domainId;
String domainGUID;

public JsonArrayData(String domainName,String domainId,String domainGUID){

    this.domainName = domainName;
    this.domainId = domainId;
    this.domainGUID = domainGUID;

}

public String getDomainName() {
    return domainName;
}

public void setDomainName(String domainName) {
    this.domainName = domainName;
}

public String getDomainId() {
    return domainId;
}

public void setDomainId(String domainId) {
    this.domainId = domainId;
}

public String getDomainGUID() {
    return domainGUID;
}

public void setDomainGUID(String domainGUID) {
    this.domainGUID = domainGUID;
}

}