用GSON序列化java对象
我想将这个对象序列化为JSON字符串用GSON序列化java对象,java,json,gson,Java,Json,Gson,我想将这个对象序列化为JSON字符串 public class Person { public String id; public String name; public Person parent; } 并得到如下结果: {id: 1, name: "Joe", parent: 2} public static class PersonSerializer implements JsonSerializer<Person> { public JsonE
public class Person {
public String id;
public String name;
public Person parent;
}
并得到如下结果:
{id: 1, name: "Joe", parent: 2}
public static class PersonSerializer implements JsonSerializer<Person> {
public JsonElement serialize(final Person person, final Type type, final JsonSerializationContext context) {
JsonObject result = new JsonObject();
result.add("id", new JsonPrimitive(person.getId()));
result.add("name", new JsonPrimitive(person.getName()));
Person parent = person.getParent();
if (parent != null) {
result.add("parent", new JsonPrimitive(parent.getId()));
}
return result;
}
}
我试着用
Person p = new Person(1, "Joe", new Person(2, "Mike"));
Gson gson = new GsonBuilder()
.registerTypeAdapter(Persona.class, new PersonSerializer()).create();
String str = gson.toJson(p);
但事实并非如此,我得到了:
"1"
PersonSerializer:
public class PersonSerializer implements JsonSerializer<Person> {
public JsonElement serialize(Person src, Type typeOfSrc, ...) {
return new JsonPrimitive(src.id);
}
}
公共类PersonSerializer实现JsonSerializer{
公共JsonElement序列化(Person src,Type of src,…){
返回新的JsonPrimitive(src.id);
}
}
欢迎提出任何建议
谢谢,
Mario为了得到您想要的结果,您需要像这样编写序列化程序:
{id: 1, name: "Joe", parent: 2}
public static class PersonSerializer implements JsonSerializer<Person> {
public JsonElement serialize(final Person person, final Type type, final JsonSerializationContext context) {
JsonObject result = new JsonObject();
result.add("id", new JsonPrimitive(person.getId()));
result.add("name", new JsonPrimitive(person.getName()));
Person parent = person.getParent();
if (parent != null) {
result.add("parent", new JsonPrimitive(parent.getId()));
}
return result;
}
}
将是
{"id":1,"name":"Joe","parent":2}
完整代码:
import java.lang.reflect.Type;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
public class GsonSimpleTest {
public static class Person {
public int id;
public String name;
public Person parent;
public Person(final int id, final String name) {
super();
this.id = id;
this.name = name;
}
public Person(final int id, final String name, final Person parent) {
super();
this.id = id;
this.name = name;
this.parent = parent;
}
public int getId() {
return id;
}
public void setId(final int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public Person getParent() {
return parent;
}
public void setParent(final Person parent) {
this.parent = parent;
}
}
public static class PersonSerializer implements JsonSerializer<Person> {
public JsonElement serialize(final Person person, final Type type, final JsonSerializationContext context) {
JsonObject result = new JsonObject();
result.add("id", new JsonPrimitive(person.getId()));
result.add("name", new JsonPrimitive(person.getName()));
Person parent = person.getParent();
if (parent != null) {
result.add("parent", new JsonPrimitive(parent.getId()));
}
return result;
}
}
public static void main(final String[] args) {
Person p = new Person(1, "Joe", new Person(2, "Mike"));
com.google.gson.Gson gson = new GsonBuilder().registerTypeAdapter(Person.class, new PersonSerializer())
.create();
System.out.println(gson.toJson(p));
}
}
import java.lang.reflect.Type;
导入com.google.gson.GsonBuilder;
导入com.google.gson.JsonElement;
导入com.google.gson.JsonObject;
导入com.google.gson.JsonPrimitive;
导入com.google.gson.JsonSerializationContext;
导入com.google.gson.JsonSerializer;
公共类GsonSimpleTest{
公共静态类人员{
公共int id;
公共字符串名称;
公众人士家长;
公众人物(最终整数id、最终字符串名称){
超级();
this.id=id;
this.name=名称;
}
公共人物(最终整数id、最终字符串名称、最终人物父级){
超级();
this.id=id;
this.name=名称;
this.parent=parent;
}
公共int getId(){
返回id;
}
公共无效集合id(最终整数id){
this.id=id;
}
公共字符串getName(){
返回名称;
}
public void setName(最终字符串名){
this.name=名称;
}
公众人物getParent(){
返回父母;
}
公共无效集合父对象(最终人员父对象){
this.parent=parent;
}
}
公共静态类PersonSerializer实现JsonSerializer{
公共JsonElement序列化(最终Person、最终Type、最终JsonSerializationContext){
JsonObject结果=新建JsonObject();
add(“id”,新的JsonPrimitive(person.getId());
add(“name”,新的JsonPrimitive(person.getName());
Person parent=Person.getParent();
如果(父项!=null){
add(“parent”,新的JsonPrimitive(parent.getId());
}
返回结果;
}
}
公共静态void main(最终字符串[]args){
人物p=新人(1,“乔”,新人(2,“迈克”);
com.google.gson.gson gson=new GsonBuilder().registerTypeAdapter(Person.class,new PersonSerializer())
.create();
System.out.println(gson.toJson(p));
}
}
你刚刚得到答案。不过,我想通过使用@JsonAdapter注释与您分享另一种方式
像这样注释Person bean
@JsonAdapter(PersonAdatper.class)
public class Person {
public int id;
public String name;
public Person parent;
}
创建自定义适配器
public class PersonAdatper extends TypeAdapter<Person> {
@Override
public void write(JsonWriter writer, Person value) throws IOException {
writer.beginObject();
writer.name("id").value(value.getId());
writer.name("name").value(value.getName());
Person parent = value.getParent();
if (parent != null) {
writer.name("parent").value(parent.getId());
}
writer.endObject();
}
@Override
public Person read(JsonReader in) throws IOException {
// do something you need
return null;
}
}
它产生如下输出:
{"id":1,"name":"Joe","parent":2}
我从教程中找到了这种方法谢谢你,斯佩斯,这是一种很好的方法,与我的案例相关,但正是它解决了这个问题