我有字符串结果=
{"fkcl":"5","fkdiv":["1","2","3","4","5"],"fkcl1":"3","fkdiv1":["1","2","3","4","5"]}
我想拆分为
[{"fkcl":"5","fkdiv":["1","2","3","4","5"]}]
[{"fkcl1":"3","fkdiv":["1","2","3","4","5"]}]
答案 0 :(得分:1)
如果您知道这些项始终以字符串"fkcl"
开头,那么您可以使用正则表达式来执行此操作。 E.g。
String json = "{\"fkcl\":\"5\",\"fkdiv\":[\"1\",\"2\",\"3\",\"4\",\"5\"],\"fkcl1\":\"3\",\"fkdiv1\":[\"1\",\"2\",\"3\",\"4\",\"5\"]}";
String output = json
.replaceAll("\\{?(\"fkcl)", "\n[{$1")
.replaceAll(",\n", "}]\n") + "]";
System.out.println(output);
产地:
[{"fkcl":"5","fkdiv":["1","2","3","4","5"]}]
[{"fkcl1":"3","fkdiv1":["1","2","3","4","5"]}]
否则使用JSON解析器(例如GSON)解析字符串,然后迭代元素并在需要时进行拆分。