从 json 字符串中删除字段
Removing fields from json string
我正在编写 api 测试。我正在使用放心通过以下方式发出请求:
public void POSTNewRequest(String endpoint, String requestBody){
response = given().auth().none()
.header("Content-Type", "application/json")
.contentType(ContentType.JSON)
.when()
.body(requestBody).log().all()
.post(endpoint);
}
我在请求中传递的 requestBody 是通过使用 ObjectMapper.writeValueAsString(requestBody).
将自定义 java 对象转换为字符串构建的
这种方法对我来说效果很好,但现在我需要在缺少某个字段的情况下发出一堆请求。例如:
{
"foo": [
{
"description": "dflt desc",
"ref": "abcd",
"FIELDTOREMOVE": 0,
"customArray": {
"number": 22,
"letter": "B"
}
}
],
"moreInfo": {
"email": "test@test.be",
"name": "Jhon Doe"
}
}
现在我想删除此请求中 post 方法之前的字段“FIELDTOREMOVE”。我尝试将 requestBody 字符串转换为 JsonNode,然后删除该字段,但它并没有删除该字段。
private void removeNullFields(String requestBody) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(requestBody);
System.out.println(jsonNode.get("FIELDTOREMOVE"));
((ObjectNode)jsonNode).remove("FIELDTOREMOVE");
}
当我尝试打印该字段的值时,它返回“null”,所以我显然做错了什么......
我也尝试通过使用具有类似结果的 gson 库来实现相同的目的,所以我想我这边存在误解,但无法弄清楚在哪里可以解决我的问题。
简而言之:我通过将字符串作为正文传递来使用放心库发出 api 请求,但在这个字符串中我有时必须删除某些字段以检查我得到的响应。
你可以使用这个库:
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
</dependency>
这样代码看起来像:
import org.json.JSONObject;
public class JSONManipulation {
static final String JSON = "{\n" +
" \"foo\": [\n" +
" {\n" +
" \"description\": \"dflt desc\",\n" +
" \"ref\": \"abcd\", \n" +
" \"FIELDTOREMOVE\": 0,\n" +
" \"customArray\": {\n" +
" \"number\": 22,\n" +
" \"letter\": \"B\"\n" +
" }\n" +
" }\n" +
" ],\n" +
" \"moreInfo\": {\n" +
" \"email\": \"test@test.be\",\n" +
" \"name\": \"Jhon Doe\"\n" +
" }\n" +
"}";
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject(JSON);
System.out.println("Value before edit: " + jsonObject.toString());
jsonObject.getJSONArray("foo").getJSONObject(0).remove("FIELDTOREMOVE");
System.out.println("Value after edit: " + jsonObject.toString());
}
}
输出将是:
Value before edit: {"foo":[{"FIELDTOREMOVE":0,"ref":"abcd","description":"dflt desc","customArray":{"number":22,"letter":"B"}}],"moreInfo":{"name":"Jhon Doe","email":"test@test.be"}}
Value after edit: {"foo":[{"ref":"abcd","description":"dflt desc","customArray":{"number":22,"letter":"B"}}],"moreInfo":{"name":"Jhon Doe","email":"test@test.be"}}
因此您可以使用 JSON 内容进行操作,然后 post 作为 String
。
您 JSON 中的“foo”是一个数组。你应该这样做:
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(requestBody);
jsonNode.get("foo").forEach(e -> ((ObjectNode) e).remove("FIELDTOREMOVE"));
System.out.println(jsonNode.toPrettyString());
输出:
{
"foo" : [ {
"description" : "dflt desc",
"ref" : "abcd",
"customArray" : {
"number" : 22,
"letter" : "B"
}
} ],
"moreInfo" : {
"email" : "test@test.be",
"name" : "Jhon Doe"
}
}
我正在编写 api 测试。我正在使用放心通过以下方式发出请求:
public void POSTNewRequest(String endpoint, String requestBody){
response = given().auth().none()
.header("Content-Type", "application/json")
.contentType(ContentType.JSON)
.when()
.body(requestBody).log().all()
.post(endpoint);
}
我在请求中传递的 requestBody 是通过使用 ObjectMapper.writeValueAsString(requestBody).
将自定义 java 对象转换为字符串构建的这种方法对我来说效果很好,但现在我需要在缺少某个字段的情况下发出一堆请求。例如:
{
"foo": [
{
"description": "dflt desc",
"ref": "abcd",
"FIELDTOREMOVE": 0,
"customArray": {
"number": 22,
"letter": "B"
}
}
],
"moreInfo": {
"email": "test@test.be",
"name": "Jhon Doe"
}
}
现在我想删除此请求中 post 方法之前的字段“FIELDTOREMOVE”。我尝试将 requestBody 字符串转换为 JsonNode,然后删除该字段,但它并没有删除该字段。
private void removeNullFields(String requestBody) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(requestBody);
System.out.println(jsonNode.get("FIELDTOREMOVE"));
((ObjectNode)jsonNode).remove("FIELDTOREMOVE");
}
当我尝试打印该字段的值时,它返回“null”,所以我显然做错了什么...... 我也尝试通过使用具有类似结果的 gson 库来实现相同的目的,所以我想我这边存在误解,但无法弄清楚在哪里可以解决我的问题。
简而言之:我通过将字符串作为正文传递来使用放心库发出 api 请求,但在这个字符串中我有时必须删除某些字段以检查我得到的响应。
你可以使用这个库:
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
</dependency>
这样代码看起来像:
import org.json.JSONObject;
public class JSONManipulation {
static final String JSON = "{\n" +
" \"foo\": [\n" +
" {\n" +
" \"description\": \"dflt desc\",\n" +
" \"ref\": \"abcd\", \n" +
" \"FIELDTOREMOVE\": 0,\n" +
" \"customArray\": {\n" +
" \"number\": 22,\n" +
" \"letter\": \"B\"\n" +
" }\n" +
" }\n" +
" ],\n" +
" \"moreInfo\": {\n" +
" \"email\": \"test@test.be\",\n" +
" \"name\": \"Jhon Doe\"\n" +
" }\n" +
"}";
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject(JSON);
System.out.println("Value before edit: " + jsonObject.toString());
jsonObject.getJSONArray("foo").getJSONObject(0).remove("FIELDTOREMOVE");
System.out.println("Value after edit: " + jsonObject.toString());
}
}
输出将是:
Value before edit: {"foo":[{"FIELDTOREMOVE":0,"ref":"abcd","description":"dflt desc","customArray":{"number":22,"letter":"B"}}],"moreInfo":{"name":"Jhon Doe","email":"test@test.be"}}
Value after edit: {"foo":[{"ref":"abcd","description":"dflt desc","customArray":{"number":22,"letter":"B"}}],"moreInfo":{"name":"Jhon Doe","email":"test@test.be"}}
因此您可以使用 JSON 内容进行操作,然后 post 作为 String
。
您 JSON 中的“foo”是一个数组。你应该这样做:
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(requestBody);
jsonNode.get("foo").forEach(e -> ((ObjectNode) e).remove("FIELDTOREMOVE"));
System.out.println(jsonNode.toPrettyString());
输出:
{
"foo" : [ {
"description" : "dflt desc",
"ref" : "abcd",
"customArray" : {
"number" : 22,
"letter" : "B"
}
} ],
"moreInfo" : {
"email" : "test@test.be",
"name" : "Jhon Doe"
}
}