Java 点的 JsonPath JUnit 转义字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19726859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:43:53  来源:igfitidea点击:

JsonPath JUnit escape character for dots

javajsonspringspring-mvcjunit

提问by cloudy_weather

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 :

我有一个名为 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));
}

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.match(JsonPathResultMatchers.java:77)
at org.springframework.test.web.servlet.MockMvc.andExpect(MockMvc.java:141)
at 

Do you know any escape character for the jsonPath?

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

采纳答案by Roman Konoval

As Idapointed out:

正如艾达指出的那样

Use brackets and quotes around your field. For example, if your field is valid.key.with.dot

Refer to it as ['valid.key.with.dot'] and in JsonPath, try

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

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

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

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

回答by Ida

Use brackets and quotes around your field. For example, if your field is valid.key.with.dot

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

Refer to it as ['valid.key.with.dot']and in JsonPath, try

称其为['valid.key.with.dot']和JsonPath,尝试

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

See this thread also: https://groups.google.com/forum/#!topic/jsonpath/7YvgXWP1_7Y

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

回答by Michael Jess

These days (e.g. io.rest-assured.json-path:3.0.1) the notation seems to be without brackets:

这些天(例如io.rest-assured.json-path:3.0.1)符号似乎没有括号:

// Some Groovy OData V4 $count test
// Response looks like:
// { "@odata.count": 2, ... }
body "'@odata.count'", equalTo(2)

I found the corresponding hint in this Git issue.

在这个 Git 问题中找到了相应的提示。