例如,构建groovy json并需要groovy builder

时间:2011-09-26 15:11:15

标签: json groovy

empData1.groovy
def empMap[] =
       [1:2,
           2:2,
       3:[1:2,3:3,4:890,A:B],  //Map inside Map
       4:4,
       6:7
      ]


empData2.groovy
def empMap1[] =
       [91:21,
           92:22,
       93:[81:82,83:3,84:890,A:B], ////Map inside Map
       94:4,
       96:7
      ]

emp3.groovy   -
  - Q1:我如何为empMap / empMap1构建构建器      - Q2:如果我想在emp3.grrovy中做的话    empData1.include(empMap1)将数据副本映射到map2

同样的事情是如何实现vis groovy json怎么样?

可以这样做

def json = new JsonBuilder()
 def result = json {
      1  2              //build a map with 1 as key 2 value without sinlge quaote is this possible
      3  33             //build a map with 3 as key 33 value without sinlge quaote is this possible
 }

println结果

**Answer
def json = new JsonBuilder()
 def result = json {
      '1'    '2'              
      '3'    '33'             
 }

println  result

我的输出 [1:2,3:33]

但我尝试构建类似

的内容
def json = new JsonBuilder()
 def result = json {
      '1'    '2'              
      '3'    '33'             
      '4'   (
          '1'   '3'
          '4'   '5'
      )
 }

println  result

它给我编译错误解决它的任何线索

1 个答案:

答案 0 :(得分:1)

Anish,我建议你试试这个来编译:

import groovy.json.JsonBuilder

def json = new JsonBuilder()
def result = json {
  '1'    '2'              
  '3'    '33'             
  '4'   {
      '1'   '3'
      '4'   '5'
  }
}
println  json