gpt4 book ai didi

java - 点的 JsonPath JUnit 转义字符

转载 作者:IT老高 更新时间:2023-10-28 13:44:34 31 4
gpt4 key购买 nike

我有一个名为 template.welcome.email 的 json 字段,我正在编写一个单元测试,检查该字段是否存在于服务器的回复中,但我找不到名称中的点的转义领域的。我的测试代码是:

@Test
public void testEmailTemplates() throws Exception {
mockMvc.perform(get("/emailTemplates")
.contentType(MediaType.APPLICATION_JSON)
.locale(Locale.UK)
.accept(MediaType.APPLICATION_JSON))

.andDo(print())
.andExpect(status().isOk())

.andExpect(jsonPath("$.template.welcome.email").exists())

.andExpect(redirectedUrl(null))
.andExpect(forwardedUrl(null));
}

但我得到以下异常,因为点被解释为路径:

java.lang.AssertionError: No value for JSON path: $.template.welcome.email, exception: invalid path
at org.springframework.test.util.JsonPathExpectationsHelper.evaluateJsonPath(JsonPathExpectationsHelper.java:74)
at org.springframework.test.util.JsonPathExpectationsHelper.exists(JsonPathExpectationsHelper.java:121)
at org.springframework.test.web.servlet.result.JsonPathResultMatchers$3.match(JsonPathResultMatchers.java:77)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:141)
at

你知道 jsonPath 的任何转义字符吗?

最佳答案

在您的字段周围使用方括号和引号。例如,如果您的字段是 valid.key.with.dot

将其称为 ['valid.key.with.dot'] 并在 JsonPath 中尝试

JsonPath.read(jsonString, "$.['valid.key.with.dot']")

另请参阅此主题:https://groups.google.com/forum/#!topic/jsonpath/7YvgXWP1_7Y

关于java - 点的 JsonPath JUnit 转义字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19726859/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com