如何将使用JsonBuilder构建的JSON附加到使用groovy的其他JSON?

如何将使用JsonBuilder构建的JSON附加到使用groovy的其他JSON?,json,groovy,Json,Groovy,关于这个问题的解决方案: 输出:- { "isOut": false, "baleRun": { "incData": true, "appendCricket": [ { "min": 10, "max": 32, "price": "10000" } ] } } 不需要外部花括号的地方(在我的情况下) 我还有另一个JSON,我需要在其中插入新创建的J

关于这个问题的解决方案:

输出:-

{
"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",
                "program": "1 Day"
            }
        ]
    },
    "fourRuns": 4,
    "sixRuns": 6
}'''
我尝试了以下代码将JSON1(使用JsonBuilder创建)添加到JSON2(需要插入的地方)的特定位置:-

 newJson.cricketDetails.cricketCategory.getAt(0).json = json
我需要的实际输出:-

{
"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
}

我怎样才能做到这一点?另外,如果我尝试上面提到的代码,则每次都会添加外部花括号,并将“json”作为键。您可以从我的输出中看到,我不需要在这里输入和键。

newJSON是字符串json是JsonBuilder的实例,其方法getContent将json作为映射返回

因此,首先需要解析newJSON以映射

然后,您可以轻松地将一个贴图插入到另一个贴图

最后从这个映射生成json作为字符串

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
def parsedJson=new JsonSlurper().parseText(newJSON)//解析到映射

cricketDetails.cricketCategory.getAt(0)newJSON是字符串json是JsonBuilder的实例,其方法getContent将json作为映射返回

因此,首先需要解析newJSON以映射

然后,您可以轻松地将一个贴图插入到另一个贴图

最后从这个映射生成json作为字符串

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
def parsedJson=new JsonSlurper().parseText(newJSON)//解析到映射

parsedJson.cricketDetails.cricketCategory.getAt(0)另一个代码,我尝试过的是-newJson.cricketDetails.cricketCategory.getAt(0).content=json.content,但仍然可以在输出中看到添加了“content”键。我不想要这个和外圆括号。我尝试过的另一个代码是-newJson.cricketDetails.cricketography.getAt(0)。content=json.content,但仍然可以在输出中看到添加了“content”键。我不想要这个和那个花括号。