Java - 如何使用正则表达式修复格式错误的JSON?

时间:2015-09-26 11:20:12

标签: java json regex parsing

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

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);
                }

并给出这样的结果:

enter image description here

但这不是有效的JSON输出。

我希望得到这样的结果。

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

我应该如何更新Regex以获得有效的JSON输出?

非常感谢您的任何建议。

1 个答案:

答案 0 :(得分: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"
}