0

I'm new in Groovy. I make map in loop like this [(nodeRootName): (value)]

And after all conversions conert it in JSON: def json = new groovy.json.JsonBuilder( map ) And this crash, because in map value not valid. Map like as [nsiKTRUs:[[position:[data:[[code:01.11.11.111-00001], [version:1], [inclusionDate:2018-06-16T05:53:14+04:00]... How I can put in quotation marks value? I try

String value = ""
value = Node.text()
map = [(nodeRootName): (value)]

And map = [(nodeRootName): ("\"" + ${value} + "\"")] and map = [(nodeRootName): ("${value.toString()}")] But map all the same [nsiKTRUs:[[position:[data:[[code:01.11.11.111-00001], [version:1], [inclusionDate:2018-06-16T05:53:14+04:00]...

  • Could you please provide the full example, that shows the problem and gives the error? What is `Node`, what is `nodeRootName`, ... – cfrick May 26 '20 at 16:00

2 Answers2

0

It's not entirely clear what you're doing, but it looks like your question is essentially just "How can I put quotation marks in a String value".

If that is the case, you can escape them (same as Java):

String value = "Hello \"world\"."

Or you can triple-quote the string:

String value = """Hello "world"."""

That said, none of this seems to be related to a map or JSON parsing so maybe you're asking something entirely different. If that's the case, please clarify.

Daniel
  • 3,312
  • 1
  • 14
  • 31
0

Instead of this:

map = [(nodeRootName): ("${value.toString()}")]

You can do this:

map = [(nodeRootName): "\"${value}\""]

Example:

groovy:000> nodeRootName = 'some root name'
===> some root name
groovy:000> 
groovy:000> value = 'some value'
===> some value
groovy:000> 
groovy:000> map = [(nodeRootName): ("${value.toString()}")]
===> [some root name:some value]
groovy:000> 
groovy:000> 
groovy:000> map = [(nodeRootName): "\"${value}\""]
===> [some root name:"some value"]
Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47