使用gson从json中删除null atributes [英] remove null atributes from json with gson

查看:227
本文介绍了使用gson从json中删除null atributes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Aiperiodo periodo = periodoService();我们可以通过使用gson来删除具有空集合或空值的属性。 
// periodo来自一个有很多值的服务方法
Gson gson = new Gson();
String json = gson.toJson(periodo);

我打印json,我有这个:

  {idPeriodo:121,codigo:2014II,
activo:false,tipoPeriodo:1,
fechaInicioPreMatricula: 2014年5月1日,
fechaFinPreMatricula:2014年1月1日,
fechaInicioMatricula:2014年5月15日,
fechaFinMatricula: ,
fechaInicioClase:ago 9,2014,
fechaFinClase:dic 14,2014,
fechaActa:ene 15,2015,
fechaUltModificacion:2014年5月28日下午12点28分26秒,
usuarioModificacion:1,aiAvisos:[],
aiAlumnoCarreraConvalidacionCursos:[],
aiAlumnoMatriculas:[],aiMallaCurriculars:[],
aiAlumnoCarreraEstados:[],aiAdmisionGrupos:[],
aiMatriculaCronogramaCabeceras:[],
aiAlumnoCarreraConvalidacions :[],
aiHorarioHorases:[],aiAsistencias:[],
aiAlumnoPreMatriculas:[],
aiAlumnoMatriculaCursoNotaDetalles:[],
aiOfertaAcademicas :[],aiTarifarios:[]}

对于那个json我不想拥有这个集合aiAvisos,有一种方法可以从json中删除它。
我正在处理大量的集合,我在这里展示了一个,我真的需要从json中移除这些。



我需要这样的东西:

  {idPeriodo:121,codigo:2014II,
activo:false,tipoPeriodo :1,
fechaInicioPreMatricula:2014年5月1日,
fechaFinPreMatricula:2014年1月1日,
fechaInicioMatricula:2014年5月15日,
fechaFinMatricula:ago 3,2014,
fechaInicioClase:ago 9,2014,
fechaFinClase:dic 14,2014,
fechaActa :ene 15,2015,
fechaUltModificacion:2014年5月28日12:28:26 PM,
usuarioModificacion:1}


我尝试将集合设置为null,我检查文档,并且没有任何方法...



请提出任何建议。
是的,我更新了我在几分钟前发布的json。



非常感谢读者!

解决方案

步骤如下:




  • 将JSON字符串转换为地图< String,Object> 使用 Gson#fromJson()

  • 迭代地图并删除code> null 或空 ArrayList

  • 表单使用


注意:使用 GsonBuilder#setPrettyPrinting()将Gson配置为输出适合漂亮打印页面的Json。

在这里找到示例代码用Gson从JSON中删除空的集合


I want to remove attributes that have empty collections or null values using gson.

Aiperiodo periodo = periodoService();
//periodo comes from a service method with a lot of values
Gson gson = new Gson();
String json = gson.toJson(periodo);

I print json and I have this:

{"idPeriodo":121,"codigo":"2014II",
"activo":false,"tipoPeriodo":1,
"fechaInicioPreMatricula":"may 1, 2014",
"fechaFinPreMatricula":"jul 1, 2014",
"fechaInicioMatricula":"jul 15, 2014",
"fechaFinMatricula":"ago 3, 2014",
"fechaInicioClase":"ago 9, 2014",
"fechaFinClase":"dic 14, 2014",
"fechaActa":"ene 15, 2015",
"fechaUltModificacion":"May 28, 2014 12:28:26 PM",
"usuarioModificacion":1,"aiAvisos":[],
"aiAlumnoCarreraConvalidacionCursos":[],
"aiAlumnoMatriculas":[],"aiMallaCurriculars":[],
"aiAlumnoCarreraEstados":[],"aiAdmisionGrupos":[],
"aiMatriculaCronogramaCabeceras":[],
"aiAlumnoCarreraConvalidacions":[],
"aiHorarioHorases":[],"aiAsistencias":[],
"aiAlumnoPreMatriculas":[],
"aiAlumnoMatriculaCursoNotaDetalles":[],
"aiOfertaAcademicas":[],"aiTarifarios":[]}

For example for that json I don't want to have the collection aiAvisos, there is a way to delete this from the json. I'm working with a lot of collections actually here I show one, I really need remove these from the json.

I need something like this:

{"idPeriodo":121,"codigo":"2014II",
"activo":false,"tipoPeriodo":1,
"fechaInicioPreMatricula":"may 1, 2014",
"fechaFinPreMatricula":"jul 1, 2014",
"fechaInicioMatricula":"jul 15, 2014",
"fechaFinMatricula":"ago 3, 2014",
"fechaInicioClase":"ago 9, 2014",
"fechaFinClase":"dic 14, 2014",
"fechaActa":"ene 15, 2015",
"fechaUltModificacion":"May 28, 2014 12:28:26 PM",
"usuarioModificacion":1}

I tried setting the collections to null, I check the documentation and there's no method there neither...

Please any suggestions. Yeah I update the json I had posted some minutes ago.

Thanks a lot who read this!

解决方案

Steps to follow:

  • Convert the JSON String into Map<String,Object> using Gson#fromJson()
  • Iterate the map and remove the entry from the map which are null or empty ArrayList.
  • Form the JSON String back from the final map using Gson#toJson().

Note : Use GsonBuilder#setPrettyPrinting() that configures Gson to output Json that fits in a page for pretty printing.

Find the sample code here Remove empty collections from a JSON with Gson

这篇关于使用gson从json中删除null atributes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆