Customizing JSON serialization in Play

后端 未结 3 841
灰色年华
灰色年华 2020-12-10 23:29

I\'m using renderJSON(Object) to return some objects as JSON values, and it\'s working fine except for one field. Is there an easy way to add in that one field

3条回答
  •  一向
    一向 (楼主)
    2020-12-10 23:59

    Simply do a

    JsonElement elem = new Gson().toJsonTree(yourObject);
    JsonObject obj = elem.getAsJsonObject();
    obj.remove("xxx");
    obj.addProperty("xxx", "what you want"); 
    // other stuff ... 
    renderJSON(obj.toString());
    

    etc.

提交回复
热议问题