您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

如何在Java中使用Gson重命名JSON的属性?

2024/5/21 5:58:14发布26次查看
gson @serializedname 注释 可以序列化为 json,并将提供的名称值作为其字段名称。此注释可以覆盖任何 fieldnamingpolicy,包括可能已在 gson 实例上设置的默认字段命名策略。可以使用gsonbuilder类设置不同的命名策略。
语法@retention(value=runtime)@target(value={field,method})public @interface serializedname
示例import com.google.gson.annotations.*;import com.google.gson.*;public class serializednameannotationtest { public static void main(string args[]) { employee emp = new employee("rahul", "dev", 30, "nagpur"); gson gson = new gsonbuilder().setprettyprinting().create(); // pretty print string jsonstr = gson.tojson(emp); system.out.println(jsonstr); }}// employee classclass employee { @serializedname("first_name") private string firstname; @serializedname("last_name") private string lastname; private int age; private string address; public employee() { } public employee(string firstname, string lastname, int age, string address) { super(); this.firstname = firstname; this.lastname = lastname; this.age = age; this.address = address; } public string getfirstname() { return firstname; } public string getlastname() { return lastname; } public int getage() { return age; } public string getaddress() { return address; }}
输出{ "first_name": "rahul", "last_name": "dev", "age": 30, "address": "nagpur"}
以上就是如何在java中使用gson重命名json的属性?的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product