2015-09-26 86 views
3

我有以下代码解析配置文件的内容。Java - 如何使用Regex修复格式错误的JSON?

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" 
} 

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

非常感谢您的任何建议。

+3

任何理由不只是要求首先配置文件是有效的JSON,然后使用普通的JSON解析器? –

+0

另外,我没有看到你在解析文件的位置。我看到的只是代码中硬编码的巨大静态字符串。您也可以首先将该字符串设置为有效的JSON。 –

回答

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" 
}