【问题标题】:Java - How to repair malformed JSON using the Regex?Java - 如何使用正则表达式修复格式错误的 JSON?
【发布时间】:2015-12-24 03:17:48
【问题描述】:

我有以下代码正在解析配置文件的内容。

String DATA = "ctrl_interface=/data/misc/wifi/sockets\n" +
                        "driver_param=use_p2p_group_interface=1\n" +
                        "update_config=1\n" +
                        "device_name=P580_ROW\n" +
                        "manufacturer=LENOVO\n" +
                        "model_name=Lenovo \n" +
                        "model_number=Lenov\n" +
                        "serial_number=hjhjh7\n" +
                        "device_type=10-0050F204-5\n" +
                        "os_version=01020300\n" +
                        "config_methods=physical_display virtual_push_button\n" +
                        "p2p_no_group_iface=1\n" +
                        "\n" +
                        "network={\n" +
                        "    ssid=\"test1\"\n" +
                        "    psk=\"154695\"\n" +
                        "    key_mgmt=WPA-PSK\n" +
                        "    sim_slot=\"-1\"\n" +
                        "    imsi=\"none\"\n" +
                        "    priority=1\n" +
                        "}\n" +
                        "\n" +
                        "network={\n" +
                        "    ssid=\"test1\"\n" +
                        "    psk=\"154695\"\n" +
                        "    key_mgmt=WPA-PSK\n" +
                        "    sim_slot=\"-1\"\n" +
                        "    imsi=\"none\"\n" +
                        "    priority=1\n" +
                        "}\n" +
                        "\n" +
                        "network={\n" +
                        "    ssid=\"test1\"\n" +
                        "    psk=\"154695\"\n" +
                        "    key_mgmt=WPA-PSK\n" +
                        "    sim_slot=\"-1\"\n" +
                        "    imsi=\"none\"\n" +
                        "    priority=1\n" +
                        "}\n" +
                        "\n" +
                        "network={\n" +
                        "    ssid=\"test1\"\n" +
                        "    psk=\"154695\"\n" +
                        "    key_mgmt=WPA-PSK\n" +
                        "    sim_slot=\"-1\"\n" +
                        "    imsi=\"none\"\n" +
                        "    priority=1\n" +
                        "}\n" +
                        "\n" +
                        "network={\n" +
                        "    ssid=\"SSID2\"\n" +
                        "    psk=\"test123456\"\n" +
                        "    key_mgmt=WPA-PSK\n" +
                        "    sim_slot=\"-1\"\n" +
                        "    imsi=\"none\"\n" +
                        "    priority=19\n" +
                        "}";

                String rel="(\\{.*?\\})";   // Curly Braces 1
                List<String> allMatches = new ArrayList<String>();
                Pattern p = Pattern.compile(rel, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
                Matcher m = p.matcher(DATA);

                while (m.find()) {
                    String foundOccurence = m.group();
                    foundOccurence = foundOccurence.replace("=", ":");
                    foundOccurence = foundOccurence.replaceAll("\\s*([^:]+):(.*(\\n|$))","\"$1\":$2");
                    allMatches.add(foundOccurence);
                }

                for(int i = 0; i < allMatches.size(); i++) {
                    String occurence = allMatches.get(i).toString();
                    Logger.d(occurence);
                }

并给出如下结果:

但这不是有效的 JSON 输出。

我想要这样的结果。

{
    "ssid": "test1",
    "psk": "154695",
    "key_mgmt": "WPA-PSK",
    "sim_slot": "-1",
    "imsi": "none",
    "priority": "1"
}

我应该如何更新正则表达式以获得有效的 JSON 输出?

非常感谢您的建议。

【问题讨论】:

  • 有什么理由不要求配置文件首先是有效的 JSON,然后使用普通的 JSON 解析器?
  • 另外,我看不到您在哪里解析文件。我所看到的只是代码中硬编码的巨大静态字符串。您还可以首先使该字符串成为有效的 JSON。

标签: java json regex parsing


【解决方案1】:

通过一些调整,您的代码可以被更改以产生您描述的输出:

String rel = "\\{(.*?)\\}";   // Curly Braces 1
List<String> allMatches = new ArrayList<String>();
Pattern p = Pattern.compile(rel, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Matcher m = p.matcher(DATA);

while (m.find()) {
    String foundOccurence = m.group(1);
    foundOccurence = foundOccurence.replaceAll("=([^\"\\n]+)", "=\"$1\"");
    foundOccurence = foundOccurence.replaceAll("\\s*([\\w]+)=(\".*\")\\n", "  \"$1\": $2,\n");
    allMatches.add("{\n" + foundOccurence.substring(0, foundOccurence.length() - 2) + "\n},\n");
}

StringBuilder builder = new StringBuilder();
for (String occurence : allMatches) {
    builder.append(occurence);
}
String result = builder.substring(0, builder.length() - 2);

result 的值变为:

{
  "ssid": "test1",
  "psk": "154695",
  "key_mgmt": "WPA-PSK",
  "sim_slot": "-1",
  "imsi": "none",
  "priority": "1"
},
{
  "ssid": "test1",
  "psk": "154695",
  "key_mgmt": "WPA-PSK",
  "sim_slot": "-1",
  "imsi": "none",
  "priority": "1"
},
{
  "ssid": "test1",
  "psk": "154695",
  "key_mgmt": "WPA-PSK",
  "sim_slot": "-1",
  "imsi": "none",
  "priority": "1"
},
{
  "ssid": "test1",
  "psk": "154695",
  "key_mgmt": "WPA-PSK",
  "sim_slot": "-1",
  "imsi": "none",
  "priority": "1"
},
{
  "ssid": "SSID2",
  "psk": "test123456",
  "key_mgmt": "WPA-PSK",
  "sim_slot": "-1",
  "imsi": "none",
  "priority": "19"
}

【讨论】:

    猜你喜欢
    • 2021-11-14
    • 2021-08-03
    • 2021-10-08
    • 2016-04-15
    • 2012-05-11
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多