Groovy inspect() handle dollar sign($)
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 \开发者_JAVA百科", it works correctly. any way, how to walk around? it's emergency for me
(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.
Use single quotes round your string:
def map = [name:'Test :: ( %2f %25 \$ * & ! @ # ^)']
When you use double quotes, the dollar char is used for templating
Use single quotes and a double backslash ,like,
def map = [name:'Test :: ( %2f %25 \\$ * & ! @ # ^)']
String s = map.inspect()
println Eval.me(s)
I just tested the following and it worked for me
> a = ["guy":"mogr \$ abi"]
Eval.me(a.inspect())["guy"]
mogr $a bi
精彩评论