<dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20160810</version> </dependency>
使用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();
}