当前位置: 代码迷 >> JavaScript >> groovy JsonBuilder将对象转向json
  详细解决方案

groovy JsonBuilder将对象转向json

热度:685   发布时间:2013-09-17 13:35:59.0
groovy JsonBuilder将对象转为json

?JsonBuilder将对象动态转为json

?

代码如下:

?

import groovy.json.*

?

def builder = new JsonBuilder()
?
??? ??? def root = builder{
??? ??? ??? "data"(
??? ??? ??? ??? LkInfo.list().collect{ lkInfo ->
??? ??? ??? ??? [
??? ??? ??? ??? ??? "city":lkInfo.city,
??? ??? ??? ??? ??? "name":lkInfo.name
??? ??? ??? ??? ??? ]
??? ??? ??? ??? }
??? ??? ??? )
??? ??? }

?

println? builder.toPrettyString();

?

结果如下:

?

{
??? "data": [
??????? {
??????????? "city": "太原",
??????????? "name": "名称1"
??????? },
??????? {
??????????? "city": "太原",
??????????? "name": "名称2"
??????? }

?? ]

}

?

?


??? ???

  相关解决方案