本文介绍了如何序列化列表< customobject>用gson?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我有一个员工类:
import java.util.List;
公共类员工{
private String name;
私人弦乐专业;
private Integer profId;
private List< String>角色;
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getProfession(){
return profession;
}
public void setProfession(String profession){
this.profession = profession;
}
public Integer getProfId(){
return profId;
}
public void setProfId(Integer profId){
this.profId = profId;
}
public List< String> getRoles(){
返回角色;
}
public void setRoles(List< String> roles){
this.roles = roles;
}
@Override
public String toString(){
String result = name +\ n+ profession +\ n
+ profId +\ n+ roles.toString();
返回结果;
}
}
我尝试为员工列表生成Json表示:
列表<员工> emps = new ArrayList<>();
员工emp1 =新员工(){
{
setName(Hassan);
setProfession(医生);
setProfId(52);
setRoles(Arrays.asList(surgeon,nose));
}};
员工emp2 =新员工(){
{
setName(Jack);
setProfession(Developer);
setProfId(52);
setRoles(Arrays.asList(Java));
}};
员工emp3 =新员工(){
{
setName(Safy);
setProfession(Driver);
setProfId(52);
setRoles(Arrays.asList(Nice,Kind));
}};
emps.add(emp1);
emps.add(emp2);
emps.add(emp3);
Gson gson = new Gson();
类型type = new TypeToken< List< Employee>>(){}。getType();
String jsonString = gson.toJson(emps,type);
生成的jsonString =[null,null,null]
在调试emps List中有3名员工按预期进行,且没有一个是空的
怎样才能正常完成?
非常感谢!
我尝试了什么:
我搜索了,
所有答案都说我应该使用TypeToken声明我做的类型但没有结果,
解决方案
Hello every one,
I have an Employee class :
import java.util.List; public class Employee { private String name; private String profession; private Integer profId; private List<String> roles; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getProfession() { return profession; } public void setProfession(String profession) { this.profession = profession; } public Integer getProfId() { return profId; } public void setProfId(Integer profId) { this.profId = profId; } public List<String> getRoles() { return roles; } public void setRoles(List<String> roles) { this.roles = roles; } @Override public String toString() { String result = name + "\n" + profession + "\n" + profId + "\n" + roles.toString(); return result; } }
I try to generate a Json representation for a list of employees :
List<Employee> emps = new ArrayList<>(); Employee emp1 = new Employee(){ { setName("Hassan"); setProfession("doctor"); setProfId(52); setRoles( Arrays.asList("surgeon" , "nose")); }}; Employee emp2 = new Employee(){ { setName("Jack"); setProfession("Developer"); setProfId(52); setRoles( Arrays.asList("Java" )); }}; Employee emp3 = new Employee(){ { setName("Safy"); setProfession("Driver"); setProfId(52); setRoles( Arrays.asList("Nice" , "Kind" )); }}; emps.add(emp1); emps.add(emp2); emps.add(emp3); Gson gson = new Gson(); Type type = new TypeToken<List<Employee>>(){}.getType(); String jsonString = gson.toJson(emps,type);
The resulting jsonString = "[null,null,null]"
in debug the emps List has the 3 employees as expected and none of them is null
How can this be properly done ?
Thanks very much!
What I have tried:
I searched,
all the answers said I should just use the TypeToken in declaring the type witch I did but with no result,
解决方案
这篇关于如何序列化列表< customobject>用gson?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!