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

JSON到Groovy解析器

SEO心得admin44浏览0评论
本文介绍了JSON到Groovy解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我发现很多关于将Groovy转换为JSON的内容,但很奇怪,不是另一种方式。

什么是最好的Groovy解析器的JSON?由于编译的Groovy类与Java类兼容,因此您应该可以使用任何Java库来将JSON转换为POJO(请参见go.microsoft/fwlink/?或POGO)。 Jackson 是一个非常受欢迎的选择,您可以使用它来转换JSON,如下所示:

字符串json ='{name:{first:Joe,last:Sixpack},gender:MALE,verified:false,userImage:Rm9vYmFyIQ ==}'

映射到一个映射:

映射< String,对象> userData = mapper.readValue(json,Map.class)

或者如果您想将JSON转换为一个Groovy用户类: $ p $ User userData = mapper.readValue(json,User.class) 这会将Groovy类中的属性映射到JSON中的键。

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

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

解决方案

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==" }'

to a Map using:

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

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

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

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

发布评论

评论列表(0)

  1. 暂无评论