-1

I have a String which I need to split and add to different arrays.

This is my String

{"locations":[{"latitude":"1.3846519","longitude":"103.763276","startTime":"1422720220292","duration":"0","accuracy":"50.981998443604"},{"latitude":"1.3845814","longitude":"103.7634384","startTime":"1422720520181","duration":"0","accuracy":"55.532001495361"},{"latitude":"1.3844195","longitude":"103.763209","startTime":"1422720820265","duration":"0","accuracy":"34.5"},{"latitude":"1.3844051","longitude":"103.7632272","startTime":"1422721120466","duration":"0","accuracy":"36"}, ],"success":1}

The output I want is like this in different arrays.

latitudeArray[] = // String array of latitude values
longitudeArray[] = // String array of longitude values
startTimeArray[] = // String array of start time values
durationArray[] = // String array of duration values
accuracyArray[] = // String array of accuracy values

I am using processing IDE to analyse my data and I tried matchAll() and split() functions but couldn't get it work. Could you please help me in getting my output? Thanks.

Edit: I managed to extract one latitude value but my method seems very inefficient. How can I do this inside a loop?

    String[] locationData = loadStrings("sample.txt");
    ArrayList<String> latitudeArray = new ArrayList<String>();
    ArrayList<String> longitudeArray = new ArrayList<String>();
    ArrayList<String> startTimeArray = new ArrayList<String>();
    ArrayList<String> durationArray = new ArrayList<String>();
    ArrayList<String> accuracyArray = new ArrayList<String>();
    String temp;
    int index;

    index = locationData[0].indexOf("latitude");
    println(index);
    temp = locationData[0].substring(index+11);
    println(temp);
    index = temp.indexOf(",");
    println(index);
    latitudeArray.add(temp.substring(0,(index-1)));
    println(latitudeArray.get(0));
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
Hasala
  • 126
  • 10
  • Post a bit of code that you tried. `"but couldn't get it work"` - why not? What was the issue you faced? – Vineet Jul 21 '15 at 02:27
  • 1
    Use a JSON parser instead of string operations. – Codebender Jul 21 '15 at 02:43
  • @Vineet please see the edit, thanks – Hasala Jul 21 '15 at 03:06
  • @Codebender I have never used that. Could you please elaborate more? – Hasala Jul 21 '15 at 03:06
  • 1
    @Hasala, Google [JSON parsing in Java](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=json%20parsing%20in%20java) – Codebender Jul 21 '15 at 03:13
  • @Codebender Thanks, Actually the data I get is a large String I got when I called a php file from processing IDE. I wonder how I would get this large String into meaningful set of variables in arrays. – Hasala Jul 21 '15 at 03:19

3 Answers3

0

Wasn't sure in what format the loadStrings() method returns, so I just used the initial String you provided.

You're heading in the right direction with the string methods. This code tries to benefit from the single input string. If you split on "latitude", then all the elemets in the array, except for the first one, will have the numbers we're interested on in the begining. E.g.: split("latitude\":\"") gives all the latitudes in the begining:

[0] = {"locations":[{"
[1] = 1.3846519","longitude":"103.763276","startTime":"1422720220292","duration":"0","accuracy":"50.981998443604"},{"
[2] = 1.3845814","longitude":"103.7634384","startTime":"1422720520181","duration":"0","accuracy":"55.532001495361"},{"
[3] = 1.3844195","longitude":"103.763209","startTime":"1422720820265","duration":"0","accuracy":"34.5"},{"
[4] = 1.3844051","longitude":"103.7632272","startTime":"1422721120466","duration":"0","accuracy":"36"}, ],"success":1}

To read the actual numbers, we just need to read until the next quote("). Doing indexOf("\"") will give use the position till which we must read to retrieve that number. So, just perform a substring(0,indexOfQuote) on it to get the value. The repeat again, but this time splitting on "longitude" to get them.

Full program:

public static void main(String[] args) {
    final String INPUT = "{\"locations\":["
            + "{\"latitude\":\"1.3846519\",\"longitude\":\"103.763276\",\"startTime\":\"1422720220292\",\"duration\":\"0\",\"accuracy\":\"50.981998443604\"},"
            + "{\"latitude\":\"1.3845814\",\"longitude\":\"103.7634384\",\"startTime\":\"1422720520181\",\"duration\":\"0\",\"accuracy\":\"55.532001495361\"},"
            + "{\"latitude\":\"1.3844195\",\"longitude\":\"103.763209\",\"startTime\":\"1422720820265\",\"duration\":\"0\",\"accuracy\":\"34.5\"},"
            + "{\"latitude\":\"1.3844051\",\"longitude\":\"103.7632272\",\"startTime\":\"1422721120466\",\"duration\":\"0\",\"accuracy\":\"36\"},"
            + " ],\"success\":1}";
    String latitudeArray[] = splitAndCollect("latitude", INPUT);
    String longitudeArray[] = splitAndCollect("longitude", INPUT);
    String startTimeArray[] = splitAndCollect("startTime", INPUT);
    String durationArray[] = splitAndCollect("duration", INPUT);
    String accuracyArray[] = splitAndCollect("accuracy", INPUT);
    System.out.println("Done");
}

private static String[] splitAndCollect(String string, String input) {
    final String COLON = "\":\"";
    String[] split = input.split(string + COLON);
    String[] output = new String[split.length - 1];
    for (int i = 0; i < output.length; i++)
        // Using [i+1] - since split[0] contains "locations".
        // Subsequent splits will have the numbers needed.
        output[i] = split[i + 1].substring(0, split[i + 1].indexOf("\""));
    System.out.println(string + "\n" + Arrays.toString(output));
    return output;
}
Vineet
  • 897
  • 10
  • 27
0

If you can preprocess the file to csv. file using simple shell script, then do string processing in java, I think you can get better performance. For csv. file processing in Java, refer http://www.mkyong.com/java/how-to-read-and-parse-csv-file-in-java/ (This blog contains simple sample).

If you do some preprocessing step (even in Java) before parsing, you can get all the values to those string arrays simply with one loop. You can use method suggested by Vineet using single loop. So with preprocessing step overall loop count becomes 2.

Thanks, Mili

0

It seems that you have data in JSON format. The way you are trying to get data from the is quite difficult (but doable). You can try JSON parser . Its easy to learn and use. You can find one example here.

Community
  • 1
  • 1
Sidharth Dash
  • 333
  • 1
  • 4
  • 13