I have the following code which is parsing the content of the config file.
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);
}
And gives result like this:

But this is not valid JSON output.
I would like to have result like this.
{
"ssid": "test1",
"psk": "154695",
"key_mgmt": "WPA-PSK",
"sim_slot": "-1",
"imsi": "none",
"priority": "1"
}
How I should to update Regex to get valid JSON output?
Many thanks for any advice.
With a few tweaks, your code can be altered to produce the output you described:
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);
The value of result becomes:
{ "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" }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With