Richie Richie - 1 year ago 181
Java Question

Convert to Json object

I have a rest endpoint in a groovy class with a method signature like this....

public PostMyResponse postMyRequest(Reader arg0) throws Exception {

Reader is a On my post request I pass in json.

If I do something like this I can see the parameters (Note: I'm aware that this is printing out strings in a java style loop. It's not what I am after)....

int value=0;

// reads to the end of the stream
while((value = != -1)
// converts int to character
char c = (char)value;

// prints character

What I am after is a way to convert the to a nice Json object using groovy and then to read properties from the json object?


Answer Source

Why not just do:

def result = new JsonSlurper().parse(reader)

Then result will be a representation (maps and lists) of whatever JSON was in your reader

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download