gson java 对象,使用gson将嵌套的Java对象与json相互转换

Let's assume we have the following two Java Classes (omitting the other class members):

class Book {

private String name;

private String[] tags;

private int price;

private Author author;

}

class Author {

private String name;

}

Furthermore, assume we have the following json object:

{"Book": {

"name": "Bible",

"price": 20,

"tags": ["God", "Religion"],

"writer": {

"name": "Jesus"

}

}

I am trying to find the best way to convert a Java Book instance to json and back using gson. To make the example more interesting, note that in json, I want to use "writer" instead of "Author". Can you please help? Ideally, I would like to see a complete implementation.

解决方案

Thanks for all the answers. I was able to figure it out and implement it using some custom serializer and deserializer. Here is my own solution:

public class JsonTranslator {

private static Gson gson = null;

public void test(Book book1) {

JsonElement je = gson.toJson(book1); // convert book1 to json

Book book2 = gson.fromJson(je, Book.class); // convert json to book2

// book1 and book2 should be equivalent

}

public JsonTranslator() {

GsonBuilder builder = new GsonBuilder();

builder.registerTypeAdapter(Book.class, new BookTrnaslator());

builder.registerTypeAdapter(Author.class, new AuthorTrnaslator());

builder.setPrettyPrinting();

gson = builder.create();

}

private class BookTrnaslator implements JsonDeserializer, JsonSerializer {

public Card deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {

JsonObject jobj = json.getAsJsonObject();

Book book = new Book();

book.setName(jobj.get("name").getAsString());

book.setTags(jobj.get("tags").getAsJsonArray()); //Assuming setTags(JsonArray ja) exists

book.setName(jobj.get("price").getAsInt());

book.setAuthor(gson.fromJson(jobj.get("writer"), Author.class));

return book;

}

public JsonElement serialize(Book src, Type typeOfSrc, JsonSerializationContext context) {

JsonObject jobj = new JsonObject();

jobj.addProperty("name", src.getName());

jobj.add("tags", src.getTagsAsJsonArray());

jobj.addProperty("price", src.getPrice());

jobj.add("writer", gson.toJson(src.getAuthor()));

return jobj;

}

}

private class AuthorTrnaslator implements JsonDeserializer, JsonSerializer {

public Card deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {

JsonObject jobj = json.getAsJsonObject();

Author author = new Author();

author.setName(jobj.get("name").getAsString());

return author;

}

public JsonElement serialize(Author src, Type typeOfSrc, JsonSerializationContext context) {

JsonObject jobj = new JsonObject();

jobj.addProperty("name", src.getName());

return jobj;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值