2

Jsonpath 对我来说是新的,我倾向于这个。我只能选择nameas $.name。我不知道如何选择这个employeenamejson type?有人能告诉我怎么做吗?

{

"name": "{\r\n  \"employeename\": \"Test  name\"\r\n}\r\n",

" availability": {

    "available": true,

    "type": "private"

},

"is_available": true
}
4

1 回答 1

1

此代码将是:

@Test
void SO_69565621() throws JsonProcessingException {
    String text = "{\n" +
            "  \"name\": \"{\\r\\n  \\\"employeename\\\": \\\"Test  name\\\"\\r\\n}\\r\\n\",\n" +
            "  \" availability\": {\n" +
            "    \"available\": true,\n" +
            "    \"type\": \"private\"\n" +
            "  },\n" +
            "  \"is_available\": true\n" +
            "}";
    String name = JsonPath.read(text, "$.name");
    JsonNode jsonNode = new ObjectMapper().readTree(name);
    MatcherAssert.assertThat(jsonNode.get("employeename").asText(), Matchers.is("Test  name"));
}

我正在使用 Jackson 将 String 转换为 JsonNode,然后获取此 JsonNode 的值。

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.12.4</version>
</dependency>
于 2021-10-14T15:42:07.500 回答