在Groovy中生成没有top标记的JSON

时间:2016-02-22 23:53:58

标签: groovy

我正在编写一个通过套接字发送内容的API。我想做的是,有这样一个基本的反应:

{
  method: ID,
  id: 10
}

第一个标签是发送的内容类型,之后的所有内容都是内容。我似乎无法弄清楚如何使用Groovy的JsonBuilder

我正在阅读documentation,我试图这样做:

String message = builder.value {
    method: Method.ID
    id: id
}

但它只生成:{value={}}。此外,它包含value标记(我不想要)。

我尝试过builder {...},但这导致了这个错误:

Exception in thread "Thread-10" groovy.lang.MissingMethodException: No signature of method: website.DerbyProManagerService.builder() is applicable for argument types: (website.DerbyProManagerService$DerbyProInstance$_newId_closure1) values: [website.DerbyProManagerService$DerbyProInstance$_newId_closure1@6db853f4]

1 个答案:

答案 0 :(得分:1)

请改为尝试:

String message = builder.call {
    method Method.ID
    id myId
}