【问题标题】:remove null keys by regex通过正则表达式删除空键
【发布时间】:2019-07-28 20:06:25
【问题描述】:

我正在尝试删除某些键的空值,我尝试使用此 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"
  }
}

我已转换为字符串,并尝试使用正则表达式删除那些空值。

【问题讨论】:

  • 你调用了某种正则表达式替换或replaceAll?
  • 为什么不解析成 POJO 忽略空值?
  • 是的,我使用的是全部替换。
  • 在发布的文本中没有出现:null,因此该表达式永远不会匹配(并且正则表达式不是最适合该工作的)
  • 我也试过那个 - private static final String REMOVE_NULL_VALS_REGEX = ",\"([^[ ]*\"]+)\": \"null\"|\"([^[ ]*\"]+)\": \"null\",|\"([^[ ]*\"]+)\": \"null\"";

标签: java json regex regex-group


【解决方案1】:

我推荐你使用 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/

【讨论】:

    【解决方案2】:

    正如 Carlos Heuberger 指出的那样,您在引号内寻找冒号,而这与任何内容都不匹配。这里(引号之间)是一个正则表达式,适合在您的上下文中用 "" 替换所有内容。

    "\"[^\"]*\"[^\"]*\"null\""
    

    逻辑是这样的。您查找一个引号,然后是除引号之外的一些内容,然后是另一个引号,除此之外的一些内容(通常是冒号和空格),然后是“null”。您用空字符串替换。唯一的缺点是您可能会留下一些不需要的逗号。假设你的字符串里面没有逗号,清理它们是通过多几行代码来实现的(碰巧,对于问题中的示例,它还清理了其他几个可疑的 json)。

    因此,以下内容将通过 RegEx 实现您的目标(这里的其他 cmets 和答案可能是正确的,这不是最好的方法——我只是按照您的想法进行)。

    // examples from question or later comment, your choice 
    //public static String jStr = "{\r\n  \"event\": \"111e0d4a\",\r\n  \"type\": \"business\",\r\n  \"eventProducerId\": \"Billing\",\r\n  \"eventVersion\": \"1.0\",\r\n  \"headerReference\": {\r\n    \"activityId\": \"999\",\r\n  },\r\n  \"payload\": {\r\n    \"trans\": \"Line\",\r\n    \"details\": {\r\n      \"plan\": {\r\n        \"features\": [\r\n          {\r\n            \"featureName\": \"GSM\",\r\n          }\r\n        ],\r\n        \"planName\": \"null\"\r\n      },\r\n      \"number\": {\r\n        \"mobileNumber\": \"111111111\"\r\n      },\r\n      \"lineType\": \"GSM\"\r\n    },\r\n    \"dea\": \"0000002\",\r\n    \"sourceBan\": \"null\",\r\n    \"financialAccount\": {\r\n      \"financialAccountNumber\": \"212121\"\r\n    },\r\n    \"reasonCode\": \"null\"\r\n  }\r\n}";
    public static String jStr = "{\"transactionType\":\"UCCLineAct\",\"dealerCode\":\"0000002\",\"financialAccount\":{\"financialAccountNumber\":\"110885364\"},\"lineOfServiceDetails\":{\"lineType\":\"GSM\",\"number\":{\"mobileNumber\":\"4079232335\"},\"ratePlan\":{\"planName\":\"null\",\"features\":[{\"featureId\":\"UCCGSM\",\"featureName\":\"UCC GSM\"}]}},\"reasonCode\":\"null\",\"sourceBan\":\"null\"} ";
    public static void main(String args[]) {
      System.out.println(jStr);  // show before we mess with it
      jStr=jStr.replaceAll("\"[^\"]*\"[^\"]*\"null\"",""); //replace "blah" :"null"
      jStr=jStr.replaceAll("(,[ \t\r\n\f]*,)+",","); //cut repeated commas separated by at most whitespace, to a single comma
      jStr=jStr.replaceAll("\\{[ \t\r\n\f]*,","\\{"); // remove comma appearing immediately (possibly with whitespace between) after {
      jStr=jStr.replaceAll("\\[[ \t\r\n\f]*,","\\["); // remove comma appearing immediately after [
      jStr=jStr.replaceAll(",[ \t\r\n\f]*\\}","\\}"); // remove comma immediately before }
      jStr=jStr.replaceAll(",[ \t\r\n\f]*\\]","\\]"); // remove comma immediately before ]
      System.out.println(jStr); // show after
    }
    

    可能有一些更巧妙的方法可以在正则表达式中包含可能的逗号,但我很难涵盖所有情况。此外,理论上 [ \t\r\n\f] 应该可以替换为 [\s],但 jdoodle 对此犹豫不决。

    【讨论】:

      猜你喜欢
      • 2012-06-03
      • 1970-01-01
      • 2011-05-11
      • 1970-01-01
      • 2011-11-01
      • 2012-11-15
      • 1970-01-01
      • 2015-07-07
      • 1970-01-01
      相关资源
      最近更新 更多