Serialize a POJO to JSON with GSON

前端 未结 2 1397
慢半拍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:03
    Pojo myPojo = new Pojo();
    
    Gson gson = new Gson();
    gson.toJson(myPojo);
    
    0 讨论(0)
  • 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));
    
    0 讨论(0)
提交回复
热议问题