如何使用groovy将使用JsonBuilder构建的JSON附加到其他JSON? [英] How to append JSON built using JsonBuilder to other JSON using groovy?

查看:696
本文介绍了如何使用groovy将使用JsonBuilder构建的JSON附加到其他JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考这个问题的解决方案: - 建筑物使用JsonBuilder的JSON

  def json = new groovy.json.JsonBuilder()
json {
isOut false
baleRun {
incData true
appendCricket([
{
min 10
max 32
price10000




$ b println json.toPrettyString()

输出: -

  {
isOut:false,
baleRun:{
incData:true,
appendCricket:[
{
min:10,
max :32,
price:10000
}
]
}
}

不需要外部大括号(在我的情况下)



我有另一个JSON,需要插入新创建的JSON: -

  def newJSON ='''{
count:6,
max:1,
bale:false,
cricketDetails:{
cricketCategory:[
{
ball :16,
swing:true,
code:2,
umpireStatus:[
{
code:TYUR ,
avail:0,
position:1,
request:

{
code :TGRE,
avail:0,
position:2,
request:
}
],
min:0,
extraCover:12,
price:DNR,
程序: 1天

]
},
fourRuns:4,
sixRuns:6
}'''

我在下面的代码中尝试将JSON1(使用JsonBuilder创建)添加到JSON2(我需要插入的地方)特殊位置: - $ / $>

  newJson.cricketDetails.cricketCategory.getAt(0).json = json 


$ b

实际产出我需要: -

  {
count:6,
max:1,
bale:false,
cricketDetails:{
cricketCategory :[{
ball:16,
swing:true,
code:2,
umpireStatus:[{
代码:TYUR,
avail:0,
position:1,
request:
},
{
code:TGRE,
avail:0,
position:2,
request:
}],
isOut:f
baleRun:{
incData:true,
appendCricket:[{
min:10,
max:32 ,
price:10000
}]
},
min:0,
extraCover:12,
价格:DNR,
程序:1天
}]
},
fourRuns:4,
sixRuns: 6
}

我该如何做到这一点?另外,如果我尝试上面提到的代码,每次使用json作为KEY时都会添加外部大括号。您可以从我的输出中看到,我不需要并在这里输入。 newJSON 是字符串。 json 是JsonBuilder的实例,它具有方法getContent,它将把json作为地图返回。

所以首先你需要解析newJSON来映射。

然后,您可以轻松地将一张地图插入其他地图。



最后从这张地图产生json作为字符串。

  def parsedJson = new JsonSlurper()。parseText(newJSON)//解析映射

parsedJson.cricketDetails.cricketCategory.getAt(0)<< json.content //修改map

def out = new JsonOutput()
println out.prettyPrint(out.toJson(parsedJson))//输出最终的json


With reference to this question's solution :- Building JSON using JsonBuilder

def json = new groovy.json.JsonBuilder()
json {
    isOut false
    baleRun {
      incData true
      appendCricket( [
      {
         min 10
         max 32      
        price "10000"
      }
     ])
   }
}

println json.toPrettyString()

Output:-

{
"isOut": false,
"baleRun": {
    "incData": true,
    "appendCricket": [
        {
            "min": 10,
            "max": 32,
            "price": "10000"
        }
       ]
    }
}

Where outside curly bracket is not required (In my case)

I have another JSON where I need to insert newly created JSON:-

def newJSON = '''{
    "count": 6,
    "max": "1",
    "bale": false,
    "cricketDetails": {
        "cricketCategory": [
            {
                "ball": 16,
                "swing": true,
                "code": "2",
                "umpireStatus": [
                    {
                        "code": "TYUR",
                        "avail": 0,
                        "position": 1,
                        "request": ""
                    },
                    {
                        "code": "TGRE",
                        "avail": 0,
                        "position": 2,
                        "request": ""
                    }
                ],
                "min": "0",
                "extraCover": 12,
                "price": "DNR",
                "program": "1 Day"
            }
        ]
    },
    "fourRuns": 4,
    "sixRuns": 6
}'''

I have tried below code to add JSON1 (which is created using JsonBuilder) to JSON2 (Where I need to insert) at particular position:-

 newJson.cricketDetails.cricketCategory.getAt(0).json = json

Actual Output I needed:-

{
"count": 6,
"max": "1",
"bale": false,
"cricketDetails": {
    "cricketCategory": [{
        "ball": 16,
        "swing": true,
        "code": "2",
        "umpireStatus": [{
            "code": "TYUR",
            "avail": 0,
            "position": 1,
            "request": ""
        },
        {
            "code": "TGRE",
            "avail": 0,
            "position": 2,
            "request": ""
        }],
        "isOut": false,
        "baleRun": {
            "incData": true,
            "appendCricket": [{
                "min": 10,
                "max": 32,
                "price": "10000"
            }]
        },
        "min": "0",
        "extraCover": 12,
        "price": "DNR",
        "program": "1 Day"
    }]
},
"fourRuns": 4,
"sixRuns": 6
}

How can I achieve this? Also, If I try above mentioned code, outer curly bracket is being added every time with "json" as KEY. You can see from my output, I do not need and key here.

解决方案

newJSON is string. json is instance of JsonBuilder that have method getContent which will return json as map.

So first of all you need to parse newJSON to map.

Then you can easily insert one map to other.

And finally produce json from this map as string.

def parsedJson = new JsonSlurper().parseText(newJSON) // parse to map

parsedJson.cricketDetails.cricketCategory.getAt(0) << json.content // modify map

def out = new JsonOutput()
println out.prettyPrint(out.toJson(parsedJson)) //output final json

这篇关于如何使用groovy将使用JsonBuilder构建的JSON附加到其他JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆