json - Groovy JSONBuilder 问题

标签 json groovy

我正在尝试将 JsonBuilder 与 Groovy 结合使用以动态生成 JSON。我想创建一个 JSON block ,例如:

{
    "type": {
        "__type": "urn",
        "value": "myCustomValue1"
    },
    "urn": {
        "__type": "urn",
        "value": "myCustomValue2"
    },
    "date": {
        "epoch": 1265662800000,
        "str": "2010-02-08T21:00:00Z"
    },
    "metadata": [{
        "ratings": [{
            "rating": "NR",
            "scheme": "eirin",
            "_type": {
                "__type": "urn",
                "value": "myCustomValue3"
            }
        }],
        "creators": [Jim, Bob, Joe]
    }]
}

我写过:

def addUrn(parent, type, urnVal) {
    parent."$type" {
        __type "urn"
        "value" urnVal
    }
}

String getEpisode(String myCustomVal1, String myCustomVal2, String myCustomVal3) {
    def builder = new groovy.json.JsonBuilder()
    def root = builder {
        addUrn(builder, "type", myCustomVal1)
        addUrn(builder, "urn", "some:urn:$myCustomVal2")
        "date" {
            epoch 1265662800000
            str "2010-02-08T21:00:00Z"
        }
       "metadata" ({
                ratings ({
                        rating "G"
                        scheme "eirin"
                        addUrn(builder, "_type", "$myCustomVal3")
                })
                creators "Jim", "Bob", "Joe"                    
        })
    }

    return root.toString();
}

但我遇到了以下问题:

  1. 每当我调用 addUrn 时,字符串中都没有返回任何内容。 我是否误解了如何在 Groovy 中使用方法?
  2. 返回的字符串中没有任何值被封装在双引号(或单引号)中。
  3. 每当我使用 { 时,我都会在返回值中得到“_getEpisode_closure2_closure2@(insert hex)”。

我的语法有问题吗?或者有人可以向我指出一些示例/教程,这些示例/教程使用了超出简单值的方法和/或示例(例如,数组中的嵌套值)。

注意:这是一个淡化的示例,但我试图保持给我带来问题的区域的复杂性。

最佳答案

  1. 你必须使用 delegateaddUrn方法代替 通过 builder您正在处理的内容。

  2. 这是因为你正在做一个toSting()toPrettyString()root而不是 builder .

  3. 如果遵循#2 则解决。

示例:

def builder = new groovy.json.JsonBuilder()
def root = builder {
              name "Devin"
              data {
                 type "Test"
                 note "Dummy"
              }
              addUrn(delegate, "gender", "male")
              addUrn(delegate, "zip", "43230")
           }


def addUrn(parent, type, urnVal) {
    parent."$type" {
        __type "urn"
        "value" urnVal
    }
}

println builder.toPrettyString()

输出:-

{
    "name": "Devin",
    "data": {
        "type": "Test",
        "note": "Dummy"
    },
    "gender": {
        "__type": "urn",
        "value": "male"
    },
    "zip": {
        "__type": "urn",
        "value": "43230"
    }
}

关于json - Groovy JSONBuilder 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17351125/

相关文章:

javascript - 为什么它不使用 jquery 在控制台日志中显示 JSON 数据?

groovy - 将文件夹从pluginBasedir复制到目标项目

grails - 如何在Grails中访问hashMap

java - SoapUI 和 Groovy : How to call a compiled jar file as if from the command line?

php - php JSON解析问题

ruby-on-rails - Ruby on Rails : send javascript array of arrays to ruby controller

Java DateTimeFormatter 从星期一开始一周

multithreading - 在 grails groovy 中运行线程

python - Tornado 应用程序/json 支持

jquery - window.location.search 查询为 JSON