如何使用JsonBuilder构造一个带有变量名和值的键的json? [英] How to construct json using JsonBuilder with key having the name of a variable and value having its value?
问题描述
如何使用具有相同名称的键和值使用JsonBuilder构造json?
How to construct json using JsonBuilder with key and value having same name?
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()
哪个会产生错误
groovy.lang.MissingMethodException:没有方法的签名: java.lang.Integer.call()适用于参数类型: (java.lang.Integer)值:[12]可能的解决方案:wait(),any(), abs(),wait(long),wait(long,int)和(java.lang.Number)
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)
引用密钥无效.任何想法如何使这项工作.
Quoting the key does has no effect. Any idea how to make this work.
我希望JSON像{ userId: 12 }
.另外,为什么不能将密钥写为字符串?
I want the JSON to be like { userId: 12 }
. Also, why does writing the key as string not work?
long userId = 12
def json = new JsonBuilder()
def root = json {
"userId" userId
}
提供的示例仅是一个片段.情况是我有很多控制器动作,这些动作已经具有各种变量.现在,我要添加一个部分,在其中尝试创建变量具有的各种值的JSON字符串.因此,更改现有的变量名称不是很实际,如果我可以用相同的名称构造JSON字符串,则将更加一致.为我想要的所有变量编写访问器方法也不是一种优雅的方法.我目前所做的是对userId
使用不同的命名方案,例如user_id
,但同样,它与我遵循的其他约定不一致.因此,我正在寻找一种优雅的方法以及JsonBuilder
如此行为的原因.
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.
如果使用JavaScript,
In case of JavaScript,
var a = 1
JSON.stringify({a: a}) // gives "{"a":1}"
这是预期的结果.
推荐答案
- 为变量
userId
声明访问器,如果您需要JSON使其类似于{userId:12}
- Declare accessors for the variable
userId
, if you need the JSON to look like{userId:12}
为
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()
- 如果您需要JSON类似于
{12:12}
,这是最简单的情况: - If you need the JSON to look like
{12:12}
which is the simplest case:
然后
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()
- 仅出于常规脚本的考虑,您可以将
def
从userId
中删除以获得第一个行为. :) - Just for the sake of the groovy script you can remove
def
fromuserId
to get the first behavior. :)
为
import groovy.json.JsonBuilder
userId = 12
def json = new JsonBuilder()
def root = json{
userId userId
}
print json.toString()
更新
在构建JSON时,局部变量也可用作映射键(默认情况下为String).
Local variables can also be used as map keys (which is String by default) while building JSON.
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"}
这篇关于如何使用JsonBuilder构造一个带有变量名和值的键的json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!