我们可以使用JsonBuilder为以下JSON创建JSON吗? [英] Can we create JSON using JsonBuilder for the following JSON?

查看:426
本文介绍了我们可以使用JsonBuilder为以下JSON创建JSON吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用JsonBuilder创建下面的JSON。

I want to create the below JSON using JsonBuilder.

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

我已经尝试了下面的代码来创建它: - $ / b
$ b

I have tried below code to create it:-

import groovy.json.*

def builder = new JsonBuilder()
def root = builder.baleRun{                
    incData true
    builder.appendCricket [
    {
        min 10
        max 32      
        price "10000"
     }
    ]                     
 }

获取以下错误: -

Getting below error:-

groovy.lang.MissingPropertyException: No such property: appendCricket for 
class: groovy.json.JsonBuilder error.

任何想法如何产生?

Any idea how to produce this?

推荐答案

最简单的方法是为您想要的数据构建一个Map,然后将其传递给构造器中的构建器:

The simplest way is to build a Map for the data you want, then pass this to the builder in the constructor:

import groovy.json.*

def data = [
    isOut: false,
    baleRun: [
        incData: true,
        appendCricket: [
            [min: 10, max: 32, price: '10']
        ]
    ]
]
def json = new JsonBuilder(data).toString()

这篇关于我们可以使用JsonBuilder为以下JSON创建JSON吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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