0

I am sending myself JSON like so with jQuery:

$.ajax
({
  type: "POST",
  url: 'http://localhost:8080/myproject/myController/myAction',
  dataType: 'json',
  async: false,
  //json object to sent to the authentication url
  data: {"stuff":"yes",
         "listThing":[1,2,3],
         "listObjects":[{"one":"thing"},{"two":"thing2"}]},
  success: function () {
    alert("Thanks!"); 
  }
})

I send this to a controller and do

println params

And I know I'm already in trouble...

[stuff:yes, listObjects[1][two]:thing2, listObjects[0][one]:thing, listThing[]:[1, 2, 3], action:myAction, controller:myController]

I cannot figure out how to get at most of these values...

I can get "yes" with params.stuff, but I cant do params.listThing.each{} or params.listObjects.each{}

What am I doing wrong?

UPDATE:

I make the controller do this to try the two suggestions so far:

println params
println params.stuff
println params.list('listObjects')
println params.listThing
def thisWontWork = JSON.parse(params.listThing)
render("omg l2json")

look how weird the parameters look at the end of the null pointer exception when I try the answers:

[stuff:yes, listObjects[1][two]:thing2, listObjects[0][one]:thing, listThing[]:[1, 2, 3], action:l2json, controller:rateAPI]
yes
[]
null
| Error 2012-03-25 22:16:13,950 ["http-bio-8080"-exec-7] ERROR   errors.GrailsExceptionResolver  - NullPointerException occurred when processing request: [POST] /myproject/myController/myAction - parameters:
stuff: yes
listObjects[1][two]: thing2
listObjects[0][one]: thing
listThing[]: 1
listThing[]: 2
listThing[]: 3

UPDATE 2 I am learning things, but this can't be right:

println params['listThing[]']
println params['listObjects[0][one]']

prints

[1, 2, 3]
thing

It seems like this is some part of grails new JSON marshaling. This is somewhat inconvenient for my purposes of hacking around with the values. How would I get all these individual params back into a big groovy object of nested maps and lists?

Maybe I am not doing what I want with jQuery?

UPDATE jQuery was the issue: Grails: where does request.JSON come from, and how do I put things there with jQuery's .ajax() or .post()?

Community
  • 1
  • 1
Mikey
  • 4,692
  • 10
  • 45
  • 73

3 Answers3

1

Sorry if 2.0 has changed this behavior, but in 1.3.7 I used the following to parse JSON data:

request.JSON.each {params  ->
...

give this a try.

dbrin
  • 15,525
  • 4
  • 56
  • 83
1

If you're sending JSON back to the controller, the best thing for you to do is just have Grails marshel it for you back into your objects.

Note the parseRequest attribute below

"/product/$id"(controller: "product", parseRequest: true) {
    action = [GET: "show", PUT: "update", DELETE: "delete", POST: "save"
}

Then in your controller it is simple as:

def save = {
    def productInstance = new Product(params.product)
}

No need to parse your params or anything. This is of course assuming you can structure your JSON to a Domain.

Gregg
  • 34,973
  • 19
  • 109
  • 214
  • This works great, but in this case I actually want to mess around with a big mess of JSON data that isn't one of my domain objects. Gonna +1, but my problem is actually in the jQuery. – Mikey Mar 28 '12 at 19:14
  • Make sure you are whitelisting which fields in Product you want to set with params.product. Otherwise this might be a security flaw. – dgtized Apr 17 '12 at 03:10
0

params always come in as Strings, so you need to do something to convert the input to an actual list. You can do this with grails.converters.JSON.parse(params.listThing).

doelleri
  • 19,232
  • 5
  • 61
  • 65
  • `println params.listThing` prints `null` and giving it to the JSON parser makes a null pointer exception. – Mikey Mar 26 '12 at 05:17