java 序列化 内部类_Gson如何序列化内部类

本文介绍了Gson如何序列化Java的成员内部类和静态内部类。通过示例代码展示了使用Gson进行序列化和反序列化的具体操作,包括创建内部类对象,设置属性,然后使用Gson转换为JSON字符串和从JSON字符串还原对象。最后,文章鼓励读者在评论区进行交流讨论。
摘要由CSDN通过智能技术生成

1 gson处理成员内部类

1.1 编写核心类

mainapp:

package com.yiidian.gson;

import com.google.gson.gson;

public class mainapp {

public static void main(string args[]) {

student student = new student();

student.setrollno(1);

student.name name = student.new name();

name.firstname = "mahesh";

name.lastname = "kumar";

student.setname(name);

gson gson = new gson();

//序列化

string jsonstring = gson.tojson(student);

system.out.println(jsonstring);

//反序列化

student = gson.fromjson(jsonstring, student.class);

system.out.println("roll no: "+ student.getrollno());

system.out.println("first name: "+ student.getname().firstname);

system.out.println("last name: "+ student.getname().lastname);

string namestring = gson.tojson(name);

system.out.println(namestring);

name = gson.fromjson(namestring,student.name.class);

system.out.println(name.getclass());

system.out.println("first name: "+ name.firstname);

system.out.println("last name: "+ name.lastname);

}

}

class student {

private int rollno;

private name name;

public int getrollno() {

return rollno;

}

public void setrollno(int rollno) {

this.rollno = rollno;

}

public name getname() {

return name;

}

public void setname(name name) {

this.name = name;

}

//成员内部类

class name {

public string firstname;

public string lastname;

}

}

1.2 运行测试

6b7ba40b1e370f881cf5b23728fb504e.png

2 gson处理静态内部类

2.1 编写核心类

mainapp:

package com.yiidian.gson;

import com.google.gson.gson;

public class mainapp {

public static void main(string args[]) {

student student = new student();

student.setrollno(1);

student.name name = new student.name();

name.firstname = "mahesh";

name.lastname = "kumar";

student.setname(name);

gson gson = new gson();

string jsonstring = gson.tojson(student);

system.out.println(jsonstring);

student = gson.fromjson(jsonstring, student.class);

system.out.println("roll no: "+ student.getrollno());

system.out.println("first name: "+ student.getname().firstname);

system.out.println("last name: "+ student.getname().lastname);

string namestring = gson.tojson(name);

system.out.println(namestring);

name = gson.fromjson(namestring,student.name.class);

system.out.println(name.getclass());

system.out.println("first name: "+ name.firstname);

system.out.println("last name: "+ name.lastname);

}

}

class student {

private int rollno;

private name name;

public int getrollno() {

return rollno;

}

public void setrollno(int rollno) {

this.rollno = rollno;

}

public name getname() {

return name;

}

public void setname(name name) {

this.name = name;

}

//静态内部类

static class name {

public string firstname;

public string lastname;

}

}

2.2 运行测试

ac9dcf9e1f54f600e0518480205aa33b.png

以上就是gson如何序列化内部类的详细内容,更多关于gson 序列化的资料请关注萬仟网其它相关文章!

希望与广大网友互动??

点此进行留言吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值