JSON到Groovy解析器 [英] JSON to Groovy parser
问题描述
我发现很多关于将Groovy转换为JSON的内容,但很奇怪,不是另一种方式。
什么是最好的Groovy解析器的JSON?由于编译的Groovy类与Java类兼容,因此您应该可以使用任何Java库来将JSON转换为POJO(请参见http://go.microsoft.com/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.
这篇关于JSON到Groovy解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!