如何在Groovy中的JSON Converter方法中保留字母大小写? [英] How to keep Letter Case in JSON Converter method in Groovy?

查看:359
本文介绍了如何在Groovy中的JSON Converter方法中保留字母大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将groovy对象解析为JSON.属性名称没有遵循正确的驼峰式大小写形式.

I'm trying to parse a groovy object to JSON. The properties names don't follow the correct camel case form.

class Client {
    String Name
    Date Birthdate
}

当我使用这个

Client client = new Client(Name: 'Richard Waters', Birthdate: new Date())
println (client as JSON).toString(true)

我明白了

"client": {
      "name": 'Richard Waters',
      "birthdate": "2016-07-22T03:00:00Z",
}

如何在属性键的开头保留大写字母?

How can I keep de Upper Case in start of my properties keys?

推荐答案

另一个选择是使用带有注释的gson serializer:

Another option is to use a gson serializer with annotations: https://google.github.io/gson/apidocs/com/google/gson/annotations/SerializedName.html

@Grab('com.google.code.gson:gson:2.7+')
import com.google.gson.Gson
import com.google.gson.annotations.SerializedName

class Client {
    @SerializedName("Name")
    String name

    @SerializedName("Birthdate")
    Date birthdate
}

def client = new Client(name: 'John', birthdate: new Date())

def strJson = new Gson().toJson(client)
println strJson

这篇关于如何在Groovy中的JSON Converter方法中保留字母大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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