如何使用具有变量名称的键和具有其值的值的 JsonBuilder 构造 json?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19225600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to construct json using JsonBuilder with key having the name of a variable and value having its value?
提问by dmahapatro
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) 值:[12] 可能的解决方案: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.
引用键没有任何效果。任何想法如何使这项工作。
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_idfor userIdbut again, it's not consistent with rest of the conventions I follow. So I am looking for an elegant approach and the reason why JsonBuilderbehaves in this manner.
提供的示例只是一个片段。情况是我有很多控制器动作,其中已经有各种变量。现在我正在添加一个部分,我试图在其中创建一个 JSON 字符串,其中包含变量保存的各种值。所以改变现有的变量名不是很实用,如果我可以构造同名的 JSON 字符串,它会更一致。为我想要的所有变量编写访问器方法也不是一种优雅的方法。我目前所做的是使用不同的命名方案user_id,userId但同样,它与我遵循的其他约定不一致。所以我正在寻找一种优雅的方法以及为什么JsonBuilder以这种方式行事的原因。
In case of JavaScript,
在 JavaScript 的情况下,
var a = 1
JSON.stringify({a: a}) // gives "{"a":1}"
which is the expected result.
这是预期的结果。
回答by dmahapatro
- Declare accessors for the variable
userId, if you need the JSON to look like{userId:12}
- 声明变量的访问器
userId,如果您需要 JSON 看起来像{userId:12}
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
deffromuserIdto get the first behavior. :)
- 只是Groovy脚本的缘故,你可以删除
def从userId拿到第一行为。:)
as
作为
import groovy.json.JsonBuilder
userId = 12
def json = new JsonBuilder()
def root = json{
userId userId
}
print json.toString()
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 = "[email protected]"
def json = new JsonBuilder()
def root = json userId: userId, age: age, email: email
print json.toString() //{"userId":12,"age":20,"email":"[email protected]"}
回答by moskiteau
import groovy.json.JsonBuilder
def userId = "12" // some user id obtained from else where.
def json = new JsonBuilder([userId: userId])
print json.toString()
回答by moskiteau
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 call(Map m)代替def call(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}

