将groovy中的json追加到json中

编程入门 行业动态 更新时间:2024-10-26 00:26:15
本文介绍了将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} } 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中

本文发布于:2023-10-27 15:24:03,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:groovy   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!