如何在Groovy中保存JSON转换器方法中的Letter Case?
发布时间:2020-12-14 16:27:35 所属栏目:大数据 来源:网络整理
导读:我正在尝试将groovy对象解析为 JSON.属性名称不遵循正确的驼峰案例形式. class Client { String Name Date Birthdate} 当我用这个 Client client = new Client(Name: 'Richard Waters',Birthdate: new Date())println (client as JSON).toString(true) 我懂
我正在尝试将groovy对象解析为
JSON.属性名称不遵循正确的驼峰案例形式.
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",} 如何在我的属性键的开头保留de Upper Case? 解决方法
另一种选择是使用带注释的gson序列化器:
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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |