我正在尝试将 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",
}
如何将大写字母保留在属性键的开头?
另一种选择是使用gson serializer
带注释:https://google.github.io/gson/apidocs/com/google/gson/annotations/SerializedName.html 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)