1

in my Java app, I have a long JSON String with data I would like to extract.

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

What I would like to extract is the Object parts (between brackets) of the String under this form "56":["81",3,"0","","","","","0","","4754","M",1] and get from them all this data: "81",3,"0","","","","","0","","4754","M",1 and be able to access their number.

To do this I've tried this:

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

The problem is I get a array out of bounds exception, and an insta-quit from this data.split("\"" + i + "\"\\:\\[")[1]. My guess is I've messed the regex up, but I can't really see where. Thanks for your help.

Alexandre Hitchcox
  • 2,704
  • 5
  • 22
  • 33

3 Answers3

4

Better use the JSON Java library. It would really simplify the parsing for you. Here's an example of how you could extract the last array that you wanted and print all its values to console.

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,

Reference:
JSON Java API docs

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
2

JSON data should be extracted with a JSON parser, never with regexes or any other kind of low-level string processing code.

Community
  • 1
  • 1
Ray Toal
  • 86,166
  • 18
  • 182
  • 232
2

It looks like you're never exiting the FOR statement because it lacks an upper bound.

for(int i = 1; i > 0; i++)

As long as "i > 0", the FOR statement will keep going, and you've started i at 1 and you're continually incrementing it, so it'll never be less than or equal to zero.

If you can grab the array size with array.length or some other method and change your FOR statement to:

for(int i = 1; i > array.length; i++)

that might resolve the issue.

Azoreo
  • 236
  • 1
  • 3
  • 14