15

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()

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)

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?

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.

In case of JavaScript,

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

which is the expected result.

4

3 回答 3

27
  • 声明变量的访问器userId,如果您需要 JSON 看起来像{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}最简单的情况:

然后

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 脚本,您可以从中删除defuserId获得第一个行为。:)

作为

import groovy.json.JsonBuilder

userId = 12

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

更新

在构建 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"}
于 2013-10-07T13:48:42.450 回答
4
import groovy.json.JsonBuilder
def userId = "12" // some user id obtained from else where.
def json = new JsonBuilder([userId: userId])
print json.toString()
于 2013-10-07T13:18:33.923 回答
3

JsonBuilder我能够使用与'scall()方法不同的参数来获得所需的输出。即,不是传入一个闭包,而是传入一个地图。

使用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}
于 2013-10-07T15:06:09.753 回答