本文介绍了如何在Grails 2.0中获得JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$。ajax
({
键入:POST,
url:'http:// localhost:8080 / myproject / myController / myAction',
dataType:'json',
async:false,
// json对象发送到认证url
data:{stuff:yes,
listThing:[1,2,3],
listObjects:[ {one:thing},{two:thing2}]},
成功:function(){
alert(Thanks!);
}
$)
我把这个发送给一个控制器并执行
println params
我知道我已经遇到麻烦了......
[stuff:是,listObjects [1] [two]:thing2,listObjects [ 0] [one]:thing,listThing []:[1,2,3],action:myAction,controller:myController]
我无法弄清楚如何获取这些值......
我可以通过params.stuff得到yes,但是我不能做par ams.listThing.each {}或params.listObjects.each {}
我做错了什么?
更新:
我让控制器执行此操作以尝试以下两个建议:
println params
println params.stuff
println params.list('listObjects')
println params.listThing
def thisWontWork = JSON.parse(params。 listThing)
render(omg l2json)
看看这些参数有多奇怪当我尝试答案时,空指针异常结束:
[stuff:是,listObjects [1] [two]:thing2 ,listObjects [0] [one]:thing,listThing []:[1,2,3],action:l2json,controller:rateAPI]
yes
[]
null
|错误2012-03-25 22:16:13,950 [http-bio-8080-exec-7] ERROR errors.GrailsExceptionResolver - 处理请求时发生NullPointerException:[POST] / myproject / myController / myAction - parameters:
stuff:是
listObjects [1] [two]:thing2
listObjects [0] [one]:thing
listThing []:1
listThing []:2
listThing []:3
更新2我正在学习东西,但这不可能是正确的:
println params ['listThing []']
println params ['listObjects [0] [one]' ]
打印
[1,2,3]
东西
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()?
解决方案
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.
这篇关于如何在Grails 2.0中获得JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!