简体   繁体   English

如何将 java 对象记录为使用 @JsonProperty 注释的 JSON

[英]How to log the java object as JSON annotated with @JsonProperty

public class Person {
   @JsonProperty("First_Name")
   private String firstName;
}

Print this object as json:将此对象打印为 json:

new Gson().toJson(person); 

This will log this object as {"firstName": "S Kumar"}这会将这个对象记录为 {"firstName": "S Kumar"}

However, I want to get it logged as {"First_Name": "S Kumar"} in the same file where it is getting created.但是,我想在创建它的同一个文件中将它记录为{"First_Name": "S Kumar"}

If it is passed over the network, then the variable will be renamed.如果它通过网络传递,那么变量将被重命名。 But, how can I log it as changed variable name.但是,如何将其记录为已更改的变量名称。

i think you can use @SerializedName("first_name").我认为你可以使用@SerializedName("first_name")。 Please refer the first link & second link请参考第一个链接第二个链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM