Grails - 导致JSONException的JSON绑定 [英] Grails - JSON binding causing JSONException

查看:117
本文介绍了Grails - 导致JSONException的JSON绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  def save(MyModel model){
model.save()
}

我正在测试它:

  //例如2ff59e55-ee3d-4f66-8bfa-00f355f52c49 
def uuid = UUID.randomUUID.toString()
controller.request.contentType = JSON_CONTENT_TYPE
controller.request.method ='POST'
controller.request.json ={'uuid':'$ uuid','description':'test object','count':1}
controller.save()

然而,每次我运行测试时,我都会得到 $ b

  org.apache.commons.lang.UnhandledException:
org.codehaus.groovy.grails.web.converters.exceptions.ConverterException:
org.codehaus.groovy.grails.web .json.JSONException:值超出序列:预期模式为
OBJECT或ARRAY写入'{'uuid':'2ff59e55-ee3d-4f66-8bfa-00f355f52c49','description':'测试对象', 'count':1}'但是INIT


解决方案

Groovy字符串上的JSON转换器扼流圈。我已经解决了这个问题:在结尾处使用 .toString()来解决这个问题:{'uuid':'$ uuid'}。toString( )


I have the following controller code:

def save(MyModel model) {
    model.save()
}

And I'm testing it using:

//e.g. 2ff59e55-ee3d-4f66-8bfa-00f355f52c49
def uuid = UUID.randomUUID.toString()
controller.request.contentType = JSON_CONTENT_TYPE
controller.request.method = 'POST'
controller.request.json = "{'uuid': '$uuid', 'description': 'test object', 'count': 1}"
controller.save()

However, every time I run the test I get,

org.apache.commons.lang.UnhandledException:
org.codehaus.groovy.grails.web.converters.exceptions.ConverterException:
org.codehaus.groovy.grails.web.json.JSONException: Value out of sequence: expected mode to be
OBJECT or ARRAY when writing '{'uuid': '2ff59e55-ee3d-4f66-8bfa-00f355f52c49', 'description': 'test object', 'count': 1}' but was INIT

解决方案

The JSON converter chokes on Groovy Strings. I've solved this by slapping a .toString() on the end: "{'uuid':'$uuid'}".toString().

这篇关于Grails - 导致JSONException的JSON绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆