如何使用JsonBuilder构造json,其键具有变量的名称和值具有其值?

时间:2021-11-13 23:47:07

How to construct json using JsonBuilder with key and value having same name?

如何使用具有相同名称的键和值的JsonBuilder构造json?

import groovy.json.JsonBuilder

def userId = 12 // some user id obtained from else where.

def json = new JsonBuilder()
def root = json {
    userId userId
}
print json.toString()

Which produces the error

哪会产生错误

groovy.lang.MissingMethodException: No signature of method: java.lang.Integer.call() is applicable for argument types: (java.lang.Integer) values: [12] Possible solutions: wait(), any(), abs(), wait(long), wait(long, int), and(java.lang.Number)

groovy.lang.MissingMethodException:没有方法签名:java.lang.Integer.call()适用于参数类型:(java.lang.Integer)values:[12]可能的解决方案:wait(),any(),abs (),wait(long),wait(long,int)和(java.lang.Number)

Quoting the key does has no effect. Any idea how to make this work.

引用密钥确实没有效果。任何想法如何使这项工作。

Thanks.

谢谢。

Edit:

编辑:

I want the JSON to be like { userId: 12 }. Also, why does writing the key as string not work?

我希望JSON像{userId:12}。另外,为什么将键作为字符串写不起作用?

long userId = 12   
def json = new JsonBuilder()
def root = json {
    "userId" userId
}

The example provided is just a snippet. The situation is that I have a lot of controller actions, which has various variables already. Now I am adding a part where I am trying to create a JSON string with various values the variables hold. So it's not very practical to change existing variable names and if I could construct the JSON string with the same name, it would be more consistent. Writing accessor methods for all the variables I wanted is also not an elegant method. What I did at present is to use different naming scheme like user_id for userId but again, it's not consistent with rest of the conventions I follow. So I am looking for an elegant approach and the reason why JsonBuilder behaves in this manner. I generally have a good opinion about Groovy but this one is very disappointing.

提供的示例只是一个片段。情况是我有很多控制器动作,它们已经有各种变量。现在我添加一个部分,我试图创建一个JSON字符串,其中包含变量所具有的各种值。因此,更改现有变量名称并不是很实际,如果我可以构造具有相同名称的JSON字符串,那么它将更加一致。为我想要的所有变量编写访问器方法也不是一种优雅的方法。我目前所做的是为userId使用不同的命名方案,如user_id,但同样,它与我遵循的其他约定不一致。所以我正在寻找一种优雅的方法以及JsonBuilder以这种方式表现的原因。我一般对Groovy有很好的看法,但这个非常令人失望。

In case of JavaScript,

如果是JavaScript,

var a = 1
JSON.stringify({a: a})    // gives "{"a":1}"

which is the expected result.

这是预期的结果。

3 个解决方案

#1


18  

  • Declare accessors for the variable userId, if you need the JSON to look like {userId:12}
  • 如果您需要JSON看起来像{userId:12},请为变量userId声明访问器

as

import groovy.json.JsonBuilder

def getUserId(){
    def userId = 12 // some user id obtained from else where.
}

def json = new JsonBuilder()
def root = json{
    userId userId
}
print json.toString()
  • If you need the JSON to look like {12:12} which is the simplest case:
  • 如果你需要JSON看起来像{12:12}这是最简单的情况:

then

然后

import groovy.json.JsonBuilder

def userId = 12 // some user id obtained from else where.

def json = new JsonBuilder()
def root = json{
    "$userId" userId
}
print json.toString()
  • Just for the sake of the groovy script you can remove def from userId to get the first behavior. :)
  • 只是为了groovy脚本,你可以从userId中删除def以获得第一个行为。 :)

as

import groovy.json.JsonBuilder

userId = 12

def json = new JsonBuilder()
def root = json{
    userId userId
}
print json.toString()

UPDATE

UPDATE

Local variables can also be used as map keys (which is String by default) while building JSON.

在构建JSON时,局部变量也可以用作映射键(默认情况下为String)。

import groovy.json.JsonBuilder

def userId = 12 
def age = 20 //For example
def email = "abc@xyz.com"

def json = new JsonBuilder()
def root = json userId: userId, age: age, email: email

print json.toString() //{"userId":12,"age":20,"email":"abc@xyz.com"}

#2


3  

import groovy.json.JsonBuilder
def userId = "12" // some user id obtained from else where.
def json = new JsonBuilder([userId: userId])
print json.toString()

#3


0  

I was able to get a desired output using a different param to JsonBuilder's call() method. i.e., instead of passing in a closure, pass in a map.

我能够使用与JsonBuilder的call()方法不同的参数获得所需的输出。即,不是通过封闭,而是通过地图。

Use def call(Map m) instead of def call(Closure c).

使用def调用(Map m)代替def调用(Closure c)。

import groovy.json.JsonBuilder

long userId = 12
long z = 12
def json = new JsonBuilder()

json userId: userId,
     abc: 1,
     z: z     
println json.toString()    //{"userId":12,"abc":1,"z":12}

#1


18  

  • Declare accessors for the variable userId, if you need the JSON to look like {userId:12}
  • 如果您需要JSON看起来像{userId:12},请为变量userId声明访问器

as

import groovy.json.JsonBuilder

def getUserId(){
    def userId = 12 // some user id obtained from else where.
}

def json = new JsonBuilder()
def root = json{
    userId userId
}
print json.toString()
  • If you need the JSON to look like {12:12} which is the simplest case:
  • 如果你需要JSON看起来像{12:12}这是最简单的情况:

then

然后

import groovy.json.JsonBuilder

def userId = 12 // some user id obtained from else where.

def json = new JsonBuilder()
def root = json{
    "$userId" userId
}
print json.toString()
  • Just for the sake of the groovy script you can remove def from userId to get the first behavior. :)
  • 只是为了groovy脚本,你可以从userId中删除def以获得第一个行为。 :)

as

import groovy.json.JsonBuilder

userId = 12

def json = new JsonBuilder()
def root = json{
    userId userId
}
print json.toString()

UPDATE

UPDATE

Local variables can also be used as map keys (which is String by default) while building JSON.

在构建JSON时,局部变量也可以用作映射键(默认情况下为String)。

import groovy.json.JsonBuilder

def userId = 12 
def age = 20 //For example
def email = "abc@xyz.com"

def json = new JsonBuilder()
def root = json userId: userId, age: age, email: email

print json.toString() //{"userId":12,"age":20,"email":"abc@xyz.com"}

#2


3  

import groovy.json.JsonBuilder
def userId = "12" // some user id obtained from else where.
def json = new JsonBuilder([userId: userId])
print json.toString()

#3


0  

I was able to get a desired output using a different param to JsonBuilder's call() method. i.e., instead of passing in a closure, pass in a map.

我能够使用与JsonBuilder的call()方法不同的参数获得所需的输出。即,不是通过封闭,而是通过地图。

Use def call(Map m) instead of def call(Closure c).

使用def调用(Map m)代替def调用(Closure c)。

import groovy.json.JsonBuilder

long userId = 12
long z = 12
def json = new JsonBuilder()

json userId: userId,
     abc: 1,
     z: z     
println json.toString()    //{"userId":12,"abc":1,"z":12}