本文介绍了将groovy中的json追加到json中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新手常规。我的要求是我必须将json添加到json.My代码中,如下所示:由我构建的JSON:
def builder = new groovy.json.JsonBuilder()
def root = builder.event {
typemodel_output_load_init
time new Timestamp date.getTime())
statussuccess
}
JSON来自DB:
def json = rs.getString(status);
现在我必须追加构建到JSON数据库中。 PLEASE帮助我解决这个问题。提前感谢。
编辑:
我构建的JSON:
{
event:{
type:model_output_load_init,
time :2015-10-01T14:08:17 + 0000,
status:success
}
}
$ b JSON FROM DB:
{
model_build :{
已初始化:{
时间戳:
}
},
modelExec:{
已初始化: {
时间戳:
}
}
}
输出需要:
{
model_build:{
已初始化: {
Timestamp:
}
},
modelExec:{
已初始化:{
Timestamp:
}
},
event:{
type:model_output_load_init,
time:2015-10-01T14:08:17 + 0000,
status:success
}
}
解决方案您可以附加到由JsonSlurper生成的地图。
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
$ b def builder = new JsonBuilder()
def root = builder.event {
typemodel_output_load_init
time new Timestamp(date.getTime())
statussuccess
}
//模拟JSON从DB
def json = new JsonSlurper()。parseText('''
{
model_build:{
已初始化:{
Timestamp:
}
},
modelExec:{
已初始化:{
时间戳:
}
}
}''')
//将构建的JSON附加到slurpedJSON
json.event = root.event
// Re-bu生成JSON,以便可以保存为字符串
新的JsonBuilder(json).toPrettyString()
{
event:{
type: model_output_load_init,
time:2015-10-01T14:39:11 + 0000,
status:success
},
modelExec :{
初始化:{
时间戳:
}
},
model_build:{
已初始化:{
时间戳:
}
}
}
I am newbie to groovy. My requirement is i have to append a json into a json.My code as follows:
JSON constructed by me:
def builder = new groovy.json.JsonBuilder() def root=builder.event{ type "model_output_load_init" time new Timestamp(date.getTime()) status "success" }
JSON from DB:
def json = rs.getString("status");
Now i have to append constructed into the JSON From DB. PLease help me to solve this.Thanks in advance.
EDIT:
My Constructed JSON:
{ "event": { "type": "model_output_load_init", "time": "2015-10-01T14:08:17+0000", "status": "success" } }
JSON FROM DB:
{ "model_build": { "Initialized": { "Timestamp": "" } }, "modelExec": { "Initialized": { "Timestamp": "" } } }
OUTPUT NEEDED:
{ "model_build": { "Initialized": { "Timestamp": "" } }, "modelExec": { "Initialized": { "Timestamp": "" } }, "event": { "type": "model_output_load_init", "time": "2015-10-01T14:08:17+0000", "status": "success" } }
解决方案You can append to the Map generated by JsonSlurper.
import groovy.json.JsonBuilder import groovy.json.JsonSlurper def builder = new JsonBuilder() def root = builder.event{ type "model_output_load_init" time new Timestamp(date.getTime()) status "success" } // Simulates the JSON from DB def json = new JsonSlurper().parseText(''' { "model_build": { "Initialized": { "Timestamp": "" } }, "modelExec": { "Initialized": { "Timestamp": "" } } }''') // Append the built JSON to the "slurped" JSON json.event = root.event // Re-build the JSON so it can saved as a String new JsonBuilder(json).toPrettyString()
The output looks like this:
{ "event": { "type": "model_output_load_init", "time": "2015-10-01T14:39:11+0000", "status": "success" }, "modelExec": { "Initialized": { "Timestamp": "" } }, "model_build": { "Initialized": { "Timestamp": "" } } }
这篇关于将groovy中的json追加到json中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!