最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JSON 到 Groovy 解析器

SEO心得admin42浏览0评论
本文介绍了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.

发布评论

评论列表(0)

  1. 暂无评论