json - Grails-在 Controller 中生成自定义JSON时出错

标签 json grails groovy

我正在尝试使用以下代码在 Controller 中创建自定义json输出,但在chrome rest客户端中收到错误“意外 token <”。相同的代码适用于xml。

def customJSON = {

    def a = Student.list().get(0)

    render(contentType:"application/json"){

        student(){ name(a.firstName) }
    }

}

def customXml = {

    def a = Student.list().get(0)

    render(contentType:"text/xml"){

        student(){ name(a.firstName) }
    }

}

最佳答案

您的代码导致以下异常:

Message: Array elements must be defined with the "element" method call eg: element(value)
    Line | Method
->>   98 | invokeMethod      in grails.web.JSONBuilder

问题是grails发送带有异常内容但内容类型为'application / json'的HTML响应。因此客户端认为这是无效的JSON响应。

下面的代码应该工作:
def a = Student.list().get(0)

render(contentType:"application/json"){
            student(name : a.firstName)
        }

关于json - Grails-在 Controller 中生成自定义JSON时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15380157/

相关文章:

ios - 将用户信息上传到 firebase 错误(swift 项目)

php json_encode 不返回任何内容

grails - 自定义新的 Grails Spring Security Core Plugin 登录页面

java - 使用 querydsl-maven-plugin 和 groovy-eclipse-compiler 生成 QueryDsl 类

java - 使用 Groovy 创建 Oracle 数据库链接

来自 JSON 的 Javascript 模板值

javascript - 从 XML 中提取数据不起作用

grails - 此M:M模式的名称

grails - 将值从 gsp 传递到 Controller

Elasticsearch : filter on scripted sum of filtered nested field