Json去除某个字段

来源:互联网 发布:大数据方向的研究生 编辑:程序博客网 时间:2024/03/29 23:18

使用Gson可以去除Json的某个字段,具体如下

需求:contentBean是个pojo对象,需要去掉id字段。

操作:

Gson gson = new Gson();

String contentGSON = gson.toJson(contentBean);
//去除id字段
try {
    JSONObject jsonObject = new JSONObject(contentGSON);
    jsonObject.remove("id");
    contentGSON = null;
    contentGSON = jsonObject.toString();
} catch (JSONException e) {
    e.printStackTrace();
}
0 0
原创粉丝点击