我想将此对象序列化为JSON String
public class Person {
public String id;
public String name;
public Person parent;
}
Run Code Online (Sandbox Code Playgroud)
并获得这样的结果:
{id: 1, name: "Joe", parent: 2}
Run Code Online (Sandbox Code Playgroud)
我试着用
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);
Run Code Online (Sandbox Code Playgroud)
但不是那样,我得到了:
"1"
Run Code Online (Sandbox Code Playgroud)
PersonSerializer:
public class PersonSerializer implements JsonSerializer<Person> {
public JsonElement serialize(Person src, Type typeOfSrc, ...) {
return new JsonPrimitive(src.id);
}
}
Run Code Online (Sandbox Code Playgroud)
欢迎任何建议
谢谢,马里奥
Fra*_*eth 61
为了得到你想要的结果,你需要像这样编写序列化器:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
结果
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));
Run Code Online (Sandbox Code Playgroud)
将会
{"id":1,"name":"Joe","parent":2}
Run Code Online (Sandbox Code Playgroud)
完整代码:
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));
}
}
Run Code Online (Sandbox Code Playgroud)
Dav*_*ham 13
你刚才得到了答案.但是,我想通过使用@JsonAdapter注释以另一种方式与您分享.
像这样注释Person bean
@JsonAdapter(PersonAdatper.class)
public class Person {
public int id;
public String name;
public Person parent;
}
Run Code Online (Sandbox Code Playgroud)
创建定制适配器
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;
}
}
Run Code Online (Sandbox Code Playgroud)
将对象序列化为json字符串
Person p = new Person(1, "Joe", new Person(2, "Mike"));
Gson gson = new Gson();
String result = gson.toJson(p);
Run Code Online (Sandbox Code Playgroud)
它产生如下输出:
{"id":1,"name":"Joe","parent":2}
Run Code Online (Sandbox Code Playgroud)
我从使用JsonAdapter的GSON Annotations示例教程中找到了这种方法
归档时间: |
|
查看次数: |
66092 次 |
最近记录: |