简体   繁体   中英

Groovy JsonSlurper Issue involving commas in JSON

I have some JSON coming into my controller, call it params.formData , it looks like this:

'{"year":"2014","resource":["Smith, John","Foo, Bar"]}'

My code to parse it:

....
def slurper = new JsonSlurper()
def data = slurper.parseText(params.formData)
...

data looks like:

[resource:["Smith", "John", "Foo", "Bar"], year:"2014"]

Notice that there were two JSON entries, and the parser made it into an array of four entries. I want it to look like this:

[resource:["Smith, John", "Foo, Bar"], year:"2014"]

Does anyone know how to handle this situation?

I don't think it does.

assert data.resource.size() == 2

Should prove me right ;-)

My guess is the output of printing data:

[resource:[Smith, John, Foo, Bar], year:2014]

Confused things. It looks like 4, but it's 2

I can't reproduce this behaviour. Run this code in the Groovy console

import groovy.json.JsonSlurper

def json = '{"resource":["Smith, John","Foo, Bar"]}'
def slurper = new JsonSlurper()
def data = slurper.parseText(json)

assert data.resource.size() == 2

The assertion passes, indicating that there are 2 entries. Why do you think there are four?

Definitely a Map rendering "optical illusion"

data.resource.each {
    println it
}

Smith, John
Foo, Bar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM