英文:
How to send array value for one the key for rest assured request
问题
{
"class": "A",
"subUsecases": [
"string1",
"String2"
]
}
To generate the above, I am creating the Array object and sending the request but its turning out to be something like this in the rest assured request.
{
"Class": "A",
"subClass": "[\"String1\",\"String2\"]"
}
Due to the above actual result, the API is treating subClass
as a string instead of an array. However, the code requires it to be an array for subClass
.
I am using a HashMap to create the above, like this:
@Test
public void mm(){
HashMap<String,String> queryParam = new HashMap<>();
queryParam.put("class", "A");
queryParam.put("subUsecases", arrayMethod("string1", "String2"));
}
public String arrayMethod(String s1, String s2){
org.json.simple.JSONArray array = new JSONArray();
array.add(s1);
array.add(s2);
return array.toJSONString();
}
queryParam
is being sent as the JSON body.
Now, how can I send the expected JSON body instead of the actual JSON body? Thanks in advance.
<details>
<summary>英文:</summary>
Below is the expected request which should hit the system but while sending it the array becoming string?
Expected
`{
"class": "A",
"subUsecases": [
"string1",
"String2"
]
}`
To generate the above, I am creating the Array object and sending the request but its turning out to be some like this in rest assured request.
Actual
{"Class": "A", "subClass": "["String1","String2"]"}
Due to above actuall result, api is thinking it a string for subClass and not treating it as array. But code wants it to be an array for subClass.
I am using hasmap to create the above. Like this
@Test
public void mm(){
HashMap<String,String> queryParam = new HashMap<>();
queryParam.put("CLASS","A");
queryParam.put("subClass", arrayMethod("String1","String2"));
}
public String arrayMethod(String s1, String s2){
org.json.simple.JSONArray array = new JSONArray();
array.add(s1);
array.add(s2);
return array.toJSONString();
}
queryParam is going as jsonbody.
Now How to send as expected json body instead of actual json body.
Thanks in advance.
</details>
# 答案1
**得分**: 4
将`Map`更改为:
```java
HashMap<String, Object> queryParam = new HashMap<>();
并且创建一个普通的列表或数组,而不是使用JSONArray
对象。array.toJSONString()
已经生成了一个JSON
字符串,你可以将其作为值放入另一个对象中。
queryParam.put("subClass", Arrays.asList("String1", "String2"));
英文:
Change Map
to:
HashMap<String, Object> queryParam = new HashMap<>();
And create a regular list or array instead of using JSONArray
object. array.toJSONString()
produces already JSON
string which you put as value to another object.
queryParam.put("subClass", Arrays.asList("String1", "String2"));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论