首页 > 解决方案 > 通过正则表达式删除空键

问题描述

我正在尝试删除某些键的空值,我尝试使用此 REGEX 但它不起作用。请你的建议...

private static final String REMOVE_NULL_VALS_REGEX = ",\"([^\"]+)\":null|\"([^\"]+)\":null,|\"([^\"]+)\":null";

{
  "event": "111e0d4a",
  "type": "business",
  "eventProducerId": "Billing",
  "eventVersion": "1.0",
  "headerReference": {
    "activityId": "999",
  },
  "payload": {
    "trans": "Line",
    "details": {
      "plan": {
        "features": [
          {
            "featureName": "GSM",
          }
        ],
        "planName": "null"
      },
      "number": {
        "mobileNumber": "111111111"
      },
      "lineType": "GSM"
    },
    "dea": "0000002",
    "sourceBan": "null",
    "financialAccount": {
      "financialAccountNumber": "212121"
    },
    "reasonCode": "null"
  }
}

我做了转换为字符串,我试图用正则表达式删除那些空值。

标签: javajsonregexregex-group

解决方案


我推荐你使用 GSON,非常简单的库

您可以使用此页面创建 pojo:http: //www.jsonschema2pojo.org/

你可以创建一个pojo(java类)

如果某个值为 null 则该类的值为 null 。

如果您想查看教程,可以查看以下链接:https ://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/


推荐阅读