JsonPath JUnit转义字符点 [英] JsonPath JUnit escape character for dots
问题描述
我有一个名为template.welcome.email的json字段,我正在编写一个单元测试,以检查服务器的回复中是否存在该字段,但是我找不到名称中的点的转义符领域的. 我测试的代码是:
I have a json field which is called template.welcome.email and I am writing a unit test that checks if that field is present in the reply from the server but I can't find an escape for the dots in the name of the field. The code of my test is :
@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));
}
但是我得到了以下异常,因为点的解释就像路径一样:
But I get the following exception, because the dots are interpreted like paths:
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的任何转义字符吗?
Do you know any escape character for the jsonPath?
推荐答案
为 Ida
在您的字段中使用方括号和引号.例如,如果您的字段是有效的.key.with.dot
Use brackets and quotes around your field. For example, if your field is valid.key.with.dot 将其引用为['valid.key.with.dot'],然后在JsonPath中尝试 Refer to it as ['valid.key.with.dot'] and in JsonPath, try 这篇关于JsonPath JUnit转义字符点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
JsonPath.read(jsonString, "$.['valid.key.with.dot']")