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

But in plain groovy, I cannot find an easy way to do this (http://groovy-lang.org/json.html)

JSONObject.fromObject(this)

return empty json strings...

Am I missing an obvious Groovy converter ? Or should I go for jackson or gson library ?

推荐答案

Do you mean like:

import groovy.json.*

class Me {
    String name
}

def o = new Me( name: 'tim' )

println new JsonBuilder( o ).toPrettyString()

Json相关问答推荐

从Razor Pages的AJAX Json呈现DataTables问题.Net GET

如何使用模式注册中心创建从主题中取消本地化的ks qlDB表?

使用Kotlin限制序列化类属性的允许整数值

使用 jq 获取所有嵌套键和值

如何使用 JOLT 将带有列表的 JSON 项目取消列出为多个项目?

使用 serde 和 csv crates 将嵌套的 json 对象序列化为 csv

将 JSON 文件放在哪里以在 Angular 8 应用程序中加载静态 JSON 数据?

在Flutter 中将 map 的 Json 转换为 list

如果 JSON 对象包含列表中的子字符串,则丢弃它们

当值包含ansible中的字符串时解析json值

json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0)

Python - 如何将 JSON 文件转换为数据框

将字符串映射到json对象的多种类型?

在自定义 JsonConverter 的 ReadJson 方法中处理空对象

JSON 使用 simplejson 序列化 Django 模型

将 javascript 对象或数组转换为 json 以获取 ajax 数据

如何让 javascript 从 .json 文件中读取?

Gson 将一组数据对象转换为 json - Android

杰克逊自动将 Joda DateTime 格式化为 ISO 8601 格式

使用 JavaScriptSerializer() 反序列化 JSON 文件