我尝试将json数组存储为在补丁图中创建的节点的属性。
public void testnewLine() {
String Value = "[{\n" +
" \"detailAccountType\": 5120,\n" +
" \"name\": \"Ventes dep roduits résiduels en France\",\n" +
" \"id\": 3,\n" +
" \"accountNumber\": \"70301\",\n" +
" \"fullyQualifiedName\": \"Ventes de produits fabriqués, prestations de services, marchandises: Ventes de produits résiduels: Ventes de produits résiduels en France\"\n" +
" }\n" +
"]";
String query = String.format("g.addV('test').property('value', '%s').project('b')", Value);
List<Result> result = helper.getQueryUtils().executeQuery(query);
Assert.assertNotNull(result);
String deleteQuery = "g.V().hasLabel('test').drop()";
helper.getQueryUtils().executeQuery(deleteQuery);
}我收到以下错误:
Script5.groovy: 1: expecting ''', found '\n' @ line 1, column 37.
g.addV('test').property('value', '[{
^发布于 2019-07-29 01:37:27
由于使用字符串API发送查询,因此必须对字符转义序列进行双重转义。例如,您可以使用Apache Commons Text中的StringEscapeUtils,但也可以使用简单的String::replaceAll:
String query = String.format("g.addV('test').property('value', '%s').project('b')",
Value.replaceAll("[\\r\\n]", "\\\\n"));但是,在您的特定示例中,最好的解决方案是根本不使用任何换行符,因为它们除了让JSON blob看起来更人眼之外,没有任何用途。我可能更喜欢用一个空格替换任何空格序列:
Value.replaceAll("[\\r\\n\\t ]+", " ")只有当JSON blob中的值使用这些字符和/或转义序列时,这才会成为问题。在这种情况下,双重转义是您唯一的选择。
https://stackoverflow.com/questions/57239050
复制相似问题