java json does not generate null or empty string attribute of detail

  • 2020-06-07 04:30:06
  • OfStack

You usually rotate json strings with java objects. null or empty string attributes do not need to be generated into json strings.

Generated as follows, the json string attribute is also generated for attributes that are not used.


JSONArray jsonarray = JSONArray.fromObject(ecmMessageMap.values());
msgObj = jsonarray.toString();


{"actionType":"","clientIp":"","ecServiceNumber":"","ecmMessageEventFlow":null,"endTime":"","eventId":"","faultEventFlowList":[],"groupId":"","psize":0,"receiver":"","receiverEcServiceNumber":"","sender":"","startTime":"","successEventIds":[]}


Gson use:


Gson g = new Gson();
msgObj = g.toJson(ecmMessage);


{"actionType":"1","successEventIds":[],"faultEventFlowList":[]}

If the big data is transferred or stored in the database, the json string can be generated reasonably, which will save a large part of storage space and transmission speed. For example, exchange monitoring mq data json format, exchange monitoring disaster recovery database storage.


Related articles: