I am struggling a bit trying to generate some JSON from a query I execute. All of the groovy JsonBuilder examples I've looked at only seem to deal with statically defining a dataset.
我正在努力尝试从我执行的查询生成一些JSON。我看过的所有groovy JsonBuilder示例似乎只是静态定义数据集。
code:
def db = new Sql(datasource)
def builder = new JsonBuilder()
db.eachRow('SELECT t.day, t.start FROM mytable') { row ->
builder.days {
day(
date row.day
)
}
}
println builder.toString()
I had it at 1 point where it was printing only the last value in the resultset out.
我在1点处得到它,它只打印结果集中的最后一个值。
Currently I am receiving the following error:
目前我收到以下错误:
unexpected token: $ @ line 46, column 18.
date row.day
I'm still a bit of a novice at groovy, any help greatly appreciated.
我在groovy仍然是一个新手,任何帮助非常感谢。
2 个解决方案
#1
1
I generally prefer to present JsonBuilder
with a complete object rather than use the DSL, so my solution would look something like this:
我通常更喜欢用完整的对象呈现JsonBuilder而不是使用DSL,所以我的解决方案看起来像这样:
def map = [days:[]]
def db = new Sql(dataSource)
db.eachRow('SELECT t.day, t.start FROM mytable') { row ->
map.days << [day : [date: row.day]]
}
println new JsonBuilder(map).toString()
If you have a large number of results, this approach has the advantage of not forcing you to compile a huge list of GroovyRowResult
objects, only a huge list of much smaller LinkedHashMap
objects.
如果你有大量的结果,这种方法的优点是不会强迫你编译一个巨大的GroovyRowResult对象列表,只是一个庞大的更小的LinkedHashMap对象列表。
#2
0
The builder there does not opening a list, add items, then close it. you would have to provide it in a single go. E.g. collect all rows as maps:
那里的构建器没有打开列表,添加项目,然后关闭它。你必须一次性提供它。例如。将所有行收集为地图:
def builder = new groovy.json.JsonBuilder()
def dbresult = [1,2,3]
builder {
days dbresult.collect{
[day: [date: it]]
}
}
println builder
Use db.rows
to get the list. You might have to try, what is happening, if you just send in the result. Maybe needs a cast to a Map or you have to do the mapping yourself.
使用db.rows获取列表。如果你只是发送结果,你可能必须尝试,发生了什么。也许需要对地图进行强制转换,或者您必须自己进行映射。
If your rowcount is very high, you might be better off some other library, that don't need you to manifest the list beforehand.
如果你的行数非常高,你可能会比其他一些库更好,你不需要预先显示列表。
#1
1
I generally prefer to present JsonBuilder
with a complete object rather than use the DSL, so my solution would look something like this:
我通常更喜欢用完整的对象呈现JsonBuilder而不是使用DSL,所以我的解决方案看起来像这样:
def map = [days:[]]
def db = new Sql(dataSource)
db.eachRow('SELECT t.day, t.start FROM mytable') { row ->
map.days << [day : [date: row.day]]
}
println new JsonBuilder(map).toString()
If you have a large number of results, this approach has the advantage of not forcing you to compile a huge list of GroovyRowResult
objects, only a huge list of much smaller LinkedHashMap
objects.
如果你有大量的结果,这种方法的优点是不会强迫你编译一个巨大的GroovyRowResult对象列表,只是一个庞大的更小的LinkedHashMap对象列表。
#2
0
The builder there does not opening a list, add items, then close it. you would have to provide it in a single go. E.g. collect all rows as maps:
那里的构建器没有打开列表,添加项目,然后关闭它。你必须一次性提供它。例如。将所有行收集为地图:
def builder = new groovy.json.JsonBuilder()
def dbresult = [1,2,3]
builder {
days dbresult.collect{
[day: [date: it]]
}
}
println builder
Use db.rows
to get the list. You might have to try, what is happening, if you just send in the result. Maybe needs a cast to a Map or you have to do the mapping yourself.
使用db.rows获取列表。如果你只是发送结果,你可能必须尝试,发生了什么。也许需要对地图进行强制转换,或者您必须自己进行映射。
If your rowcount is very high, you might be better off some other library, that don't need you to manifest the list beforehand.
如果你的行数非常高,你可能会比其他一些库更好,你不需要预先显示列表。