【问题标题】:Removing fields from json string从 json 字符串中删除字段
【发布时间】:2021-10-29 07:51:56
【问题描述】:

我正在编写 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 请求,但在此字符串中,我有时必须删除某些字段以检查我得到的响应。

【问题讨论】:

    标签: java json jackson rest-assured


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      你可以使用这个库:

      <!-- 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 内容进行操作,然后以 String 的形式发布。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-26
        相关资源
        最近更新 更多