gpt4 book ai didi

java - Grails json 使用列表

转载 作者:行者123 更新时间:2023-12-02 06:39:55 25 4
gpt4 key购买 nike

我的域类声明为 我正在使用 Grails 1.3.7

StudentReport{
String username
String studentName
}

def sp = StudentReport.createCriteria()



def studentReportList = sp.list {

projections {
property 'username'
property 'studentName'
}
firstResult 0
maxResults 20
order("id", "asc")
}
render studentReportList as JSON

My expected result is

[
{"username":"13B06","student_name":"JOHN SAMUEL"},
{"username":"13B07","student_name":"KATHIRESAN.K"},
{"username":"13B08","student_name":"KATHIRESAN.K"},
{"username":"13B11","student_name":"MALINI.S"}
]

But what i get is

[
["13B06","JOHN SAMUEL"],
["13B07","KATHIRESAN.K"],
["13B08","KATHIRESAN.K"],
["13B11","MALINI.S"]
]

我需要带有大括号的属性名称。任何人都可以帮助我吗?我尝试过在 Controller 中不使用投影,然后我得到了准确的结果,但如果我尝试使用投影,那么我就无法得到相同的结果。

最佳答案

[ {"13B06","JOHN SAMUEL"},{"13B07","KATHIRESAN.K"},{"13B08","KATHIRESAN.K"},{"13B11","MALINI.S"} ]

不是有效的 JSON。您当前获得的输出是正确的,因为 studentReportList 是列表的列表,因此 JSON 必须是数组的数组。如果您想要大括号,那么内部元素需要是 JSON 对象 而不是数组。生成

[
{"username":"13B06","student_name":"JOHN SAMUEL"},
...
]

您可以向您的条件添加合适的结果转换器,并使用 property 投影方法的双参数形式来分配正确的别名:

def studentReportList = sp.list{
projections {
property 'username', 'username'
property 'studentName', 'student_name'
}
firstResult 0
maxResults 20
order("id", "asc")
resultTransformer AliasToEntityMapResultTransformer.INSTANCE
}

(与 appropriate import 一起)使条件返回 map 列表而不是列表列表。

关于java - Grails json 使用列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19249908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com