Groovy - 将对象转换为JSON字符串 [英] Groovy - Convert object to JSON string
本文介绍了Groovy - 将对象转换为JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我非常习惯Grails转换器,您可以将任何对象转换为JSON表示形式,就像这样( http ://grails.org/Converters+Reference )
I'm pretty used to Grails converters, where you can convert any object to a JSON representation just like this (http://grails.org/Converters+Reference)
return foo as JSON
但是在普通的groovy中,我找不到一个简单的方法来执行此操作( http://groovy-lang.org/json.html )
But in plain groovy, I cannot find an easy way to do this (http://groovy-lang.org/json.html)
JSONObject.fromObject(this)
返回空的json字符串...
return empty json strings...
我错过了一个明显的Groovy转换器吗?
或者我应该去找杰克逊还是gson库?
Am I missing an obvious Groovy converter ? Or should I go for jackson or gson library ?
推荐答案
你的意思是:
import groovy.json.*
class Me {
String name
}
def o = new Me( name: 'tim' )
println new JsonBuilder( o ).toPrettyString()
这篇关于Groovy - 将对象转换为JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文