gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:57:33 26 4
gpt4 key购买 nike

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

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 输出?

非常感谢您的任何建议。

最佳答案

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

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

关于Java - 如何使用正则表达式修复格式错误的 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32796600/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com