Groovyspect() 处理美元符号($)

发布于 2024-12-04 17:14:33 字数 470 浏览 1 评论 0原文

下面的代码无法运行

def map = [name:"Test :: ( %2f %25 \$ * & ! @ # ^)"]
String s = map.inspect()
println Eval.me(s)

得到错误:

Script1.groovy: 1: illegal string body character after dollar sign; 

solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}" @ line 1, column 30.
   ["name":"Test :: ( %2f %25 $ * & ! @ # ^)"]

但如果字符串包含其他特殊字符,如\“,它可以正常工作。无论如何,如何走动?这对我来说很紧急

below code can not run

def map = [name:"Test :: ( %2f %25 \$ * & ! @ # ^)"]
String s = map.inspect()
println Eval.me(s)

get error:

Script1.groovy: 1: illegal string body character after dollar sign; 

solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}" @ line 1, column 30.
   ["name":"Test :: ( %2f %25 $ * & ! @ # ^)"]

but if string contain other special char like \", it works correctly. any way, how to walk around? it's emergency for me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

々眼睛长脚气 2024-12-11 17:14:33

(回应上面的后续)

好的,如果您只是想交换信息,那么您应该使用数据交换格式,例如 XML 或 JSON。我推荐 JSON,因为它轻量、快速且确实易于使用:

计算机 1

def map = [name:"Test :: ( %2f %25 \$ * & ! @ # ^)"]
def json = new groovy.json.JsonBuilder()
json(map)
println json.toString()

计算机 2

def incoming = '{"name":"Test :: ( %2f %25 $ * & ! @ # ^)"}'
def jsonInput = new groovy.json.JsonSlurper()
def map = jsonInput.parseText(incoming)
println map

请注意,这些需要 Groovy 1.8.0 或功能较新。对于旧版本的 Groovy 有很多示例,Grails 也有自己的内置解析器。

(In response to follow up above)

OK, if you just want to exchange information, then you should use a Data Interchange Format, such as XML or JSON. I recommend JSON because it's lightweight, fast, and really easy to use:

Computer 1

def map = [name:"Test :: ( %2f %25 \$ * & ! @ # ^)"]
def json = new groovy.json.JsonBuilder()
json(map)
println json.toString()

Computer 2

def incoming = '{"name":"Test :: ( %2f %25 $ * & ! @ # ^)"}'
def jsonInput = new groovy.json.JsonSlurper()
def map = jsonInput.parseText(incoming)
println map

Please note that these require Groovy 1.8.0 or newer to function. There are plenty of examples for older versions of Groovy, and Grails has it's own parsers built-in as well.

洛阳烟雨空心柳 2024-12-11 17:14:33

在字符串周围使用单引号:

def map = [name:'Test :: ( %2f %25 \$ * & ! @ # ^)']

当您使用双引号时,美元字符用于模板化

Use single quotes round your string:

def map = [name:'Test :: ( %2f %25 \$ * & ! @ # ^)']

When you use double quotes, the dollar char is used for templating

探春 2024-12-11 17:14:33

使用单引号和双反斜杠,例如,

def map = [name:'Test :: ( %2f %25 \\$ * & ! @ # ^)']
String s = map.inspect()
println Eval.me(s)

Use single quotes and a double backslash ,like,

def map = [name:'Test :: ( %2f %25 \\$ * & ! @ # ^)']
String s = map.inspect()
println Eval.me(s)
静待花开 2024-12-11 17:14:33

我刚刚测试了以下内容,它对我有用

> a = ["guy":"mogr \$ abi"]
Eval.me(a.inspect())["guy"]
mogr $a bi

I just tested the following and it worked for me

> a = ["guy":"mogr \$ abi"]
Eval.me(a.inspect())["guy"]
mogr $a bi
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文