JSON 到 Groovy 解析器 [英] JSON to Groovy parser

查看:22
本文介绍了JSON 到 Groovy 解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了很多关于将 Groovy 转换为 JSON 的事情,但很奇怪,不是相反.

I found many things about converting Groovy to JSON, but oddly enough, not the other way.

那里最好的 JSON 到 Groovy 解析器是什么?

What is the (best) JSON to Groovy parser around there ?

推荐答案

因为编译的 Groovy 类与 Java 类兼容,所以您应该能够使用任何 Java 库将 JSON 转换为 POJO(或 POGO).Jackson 是一个相当流行的选择,您可以使用它来像这样转换 JSON:

Because compiled Groovy classes are compatible with Java classes, you should be able to use any Java library for converting JSON to POJOs (or POGOs). Jackson is a fairly popular choice which you can use to convert JSON like this:

String json = '{
  "name" : { "first" : "Joe", "last" : "Sixpack" },
  "gender" : "MALE",
  "verified" : false,
  "userImage" : "Rm9vYmFyIQ=="
}'

到地图使用:

Map<String,Object> userData = mapper.readValue(json, Map.class)

或者如果您想将 JSON 转换为 Groovy User 类:

Or if you want to convert the JSON to a Groovy User class:

User userData = mapper.readValue(json, User.class)

这会将 Groovy 类中的属性映射到 JSON 中的键.

This will map properties in the Groovy class to keys in the JSON.

这篇关于JSON 到 Groovy 解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆