Serialize a POJO to JSON with GSON

前端 未结 2 1469
慢半拍i
慢半拍i 2021-01-01 16:36

I pull in some JSON, deserialize it to a POJO, edit some properties of the object, and now I want to serialize it back to JSON with GSON and send it back.

How do i

2条回答
  •  感情败类
    2021-01-01 17:06

    The gson.toJson() returns a String, not a JsonObject.

    If you want to get a real JsonObject better do this:

    JsonObject json = (JsonObject)parser.parse(new Gson().toJson(myPojo));
    

提交回复
热议问题