json - Grails - JSON 绑定(bind)导致 JSONException

标签 json grails groovy

我有以下 Controller 代码:

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

我正在使用以下方法对其进行测试:
//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()

但是,每次我运行测试时,
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

最佳答案

JSON 转换器在 Groovy 字符串上阻塞。我通过拍.toString() 解决了这个问题最后:"{'uuid':'$uuid'}".toString() .

关于json - Grails - JSON 绑定(bind)导致 JSONException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24040673/

相关文章:

javascript - XML XPath 和 XSLT 的 JSON 等价物是什么?

grails - Grails:withFormat在 Action 方法中间

grails - 使用 spring-security-core 插件将 Facebook 登录 (oauth) 与现有的 Grails 应用程序集成

sql - 如何在Linux/Grails/Groovy环境中的Grails h2数据源中 “look at data”?

json - JSON到Groovy解析器

javascript - 在 Angular JS 1.6 中处理 JSONP 响应

json - 通过 JSON 网络请求发送 Onesignal/gamethrive 推送

web-services - Java EE 和 Grails : Communication with WebServices? 哪个框架?

jenkins - 在 System Groovy 中暂时禁用 Jenkins 服务器上的 SCM 轮询

php - 使用 json_encode,有没有办法强制某些值不被转换为数字?