在我的Java应用程序中,我有一个很长的JSON字符串,其中包含我想要提取的数据。
[["19:05",2,"","19.3","26.7","74","1003.6","3.2","236.0","00 : 05 : 43","01 : 54 : 17","","","",null,"rain"],{"1":["2",3,"0","","","","","0","","4713","M",1],"2":["3",3,"0","","","","","0","","4714","M",1],"3":["3681",3,"0","","","","","0","","4715","M",1],"4":["28",4,"0","","","","","0","","4716","M",1],"5":["7",4,"0","","","","","1","","4717","M",1],"6":["13",3,"0","","","","","0","","4718","M",1],"7":["35",4,"0","","","","","0","","4719","M",0],"8":["82",4,"0","","","","","0","","4720","M",1],"9":["44",4,"0","","","","","0","","4721","D",1],"10":["193",4,"0","","","","","0","","4722","D",1],"11":["3347",3,"0","","","","","0","","4723","D",1],"12":["144",4,"0","","","","","0","","4724","D",1],"13":["90",4,"0","","","","","0","","4726","D",0],"14":["89",4,"0","","","","","1","","4727","D",1],"15":["3582",2,"1","","","","5:36.746","0","","4728","D",1],"16":["64",4,"0","","","","","0","","4729","M",0],"17":["78",4,"0","","","","","0","","4730","D",0],"18":["3577",4,"0","","","","","0","","4731","D",1],"19":["38",3,"0","","","","","0","","4732","M",0],"20":["3686",3,"0","","","","","0","","4733","D",0],"21":["1351",4,"0","","","","","0","","4772","D",0],"22":["3689",4,"0","","","","","0","","4734","D",0],"23":["104",4,"0","","","","","0","","4735","D",1],"24":["3693",4,"0","","","","","0","","4736","D",1],"25":["249",4,"0","","","","","0","","4769","D",0],"26":["16",4,"0","","","","","0","","4738","D",1],"27":["171",4,"0","","","","","0","","4739","D",0],"28":["3670",4,"0","","","","","0","","4740","M",0],"29":["3588",4,"0","","","","","0","","4741","D",0],"30":["77",4,"0","","","","","0","","4742","M",1],"31":["151",4,"0","","","","","0","","4755","M",1],"32":["53",4,"0","","","","","0","","4743","M",1],"33":["267",4,"0","","","","","0","","4751","M",0],"34":["3671",4,"0","","","","","0","","4756","M",1],"35":["155",4,"0","","","","","0","","4757","M",1],"36":["153",4,"0","","","","","0","","4758","M",1],"37":["3672",4,"0","","","","","0","","4759","M",1],"38":["3328",4,"0","","","","","0","","4744","D",0],"39":["130",4,"0","","","","","0","","4760","M",1],"40":["3677",4,"0","","","","","0","","4761","M",1],"41":["97",4,"0","","","","","0","","4745","M",1],"42":["101",4,"0","","","","","0","","4746","M",0],"43":["102",3,"0","","","","","0","","4747","M",0],"44":["40",3,"0","","","","","0","","4819","M",0],"45":["129",4,"0","","","","","0","","4762","M",1],"46":["2777",4,"0","","","","","0","","4763","M",0],"47":["3352",4,"0","","","","","0","","4764","M",1],"48":["169",4,"0","","","","","0","","4765","M",1],"49":["120",4,"0","","","","","0","","4748","M",1],"50":["137",3,"0","","","","","0","","4749","M",1],"51":["3678",4,"0","","","","","0","","4750","M",0],"52":["87",3,"0","","","","","0","","4766","M",1],"53":["299",3,"0","","","","","0","","4767","M",1],"54":["19",3,"0","","","","","0","","4752","M",1],"55":["333",4,"0","","","","","1","","4753","M",1],"56":["81",3,"0","","","","","0","","4754","M",1]}]
我想要提取的是此格式"56":["81",3,"0","","","","","0","","4754","M",1]
下字符串的对象部分(括号内),并从中获取所有这些数据:"81",3,"0","","","","","0","","4754","M",1
并能够访问其编号。< / p>
要做到这一点,我试过这个:
for(int i = 1; i > 0; i++) {
String d;
try {
d = data.split("\"" + i + "\"\\:\\[")[1];
d = d.split("\\]")[0];
} catch(Exception e) {
e.printStackTrace();
System.out.println("quit");
break;
}
}
问题是我得到一个数组超出范围的异常,以及来自此data.split("\"" + i + "\"\\:\\[")[1]
的insta-quit。我的猜测是我把正则表达式搞砸了,但我真的看不到哪里。谢谢你的帮助。
答案 0 :(得分:4)
更好地使用JSON Java库。它会真正简化您的解析。下面是一个示例,说明如何提取所需的最后一个数组并将其所有值打印到控制台。
final JSONArray root = new JSONArray(YOUR_JSON_STRING);
final JSONObject numData = root.getJSONObject(1);
final JSONArray numArray = numData.getJSONArray("56");
for (int i = 0; i < numArray.length(); ++i) {
if (i == 1 || i == 11)
System.out.print(numArray.getInt(i)+",");
else
System.out.print(numArray.getString(i)+",");
}
// prints: "81",3,"0","","","","","0","","4754","M",1,
的参考强>:
JSON Java API docs
答案 1 :(得分:2)
JSON数据应该使用JSON parser提取,绝不能使用正则表达式或任何其他类型的低级字符串处理代码。
答案 2 :(得分:2)
看起来你永远不会退出FOR语句,因为它没有上限。
for(int i = 1; i&gt; 0; i ++)
只要“i&gt; 0”,FOR语句就会继续,并且你已经从1开始,并且你不断地递增它,所以它永远不会小于或等于零。 / p>
如果您可以使用array.length或其他方法获取数组大小,并将FOR语句更改为:
for(int i = 1; i&gt; array.length; i ++)
可能会解决问题。