0

I want to know that how to convert a string from Jsonarray, like the one in the following code, to a list of doubles.

String lineStringJsonArray = "[[[0.093493,51.6037],[0.092077,51.6134],[0.075051,51.6179],[-0.247248,51.5166],[-0.259754,51.5235],[-0.28098,51.518],[-0.301457,51.515]]]"

How should I use pattern to drop those square brackets?

  • Take a look at this question to get started: http://stackoverflow.com/questions/8264267/decode-json-with-java Also, please read how to format code in your question to bake it more readable for others :) – Richard87 Jul 31 '15 at 23:07
  • Looks like a pretty simple `double[][][]` to me, and any mapper should handle that. A bit more helpful might be `List>`, since those look like latitude/longitude pairs. – chrylis -cautiouslyoptimistic- Jul 31 '15 at 23:08
  • Your list of double seems to be more like a list of double tuples, or double pairs to be specific. Also it seems to be one additional level deep (list-of-list-of-double-pairs). – YoYo Jul 31 '15 at 23:09
  • Thanks for all the comments! actually, I want to use a pattern to extract those pair latitude/longitude to a list of latitude/longitude. Could someone help? – HaridingzLiu Aug 01 '15 at 05:50

3 Answers3

2

Use json-simple.

Use the JSONArray object that returns an array-of-objects-like object to iterate over. I've compiled an example of printing all the doubles in the arrays, if that's what you wanted.

String lineStringJsonArray = "[[[0.093493,51.6037],[0.092077,51.6134],[0.075051,51.6179],[-0.247248,51.5166],[-0.259754,51.5235],[-0.28098,51.518],[-0.301457,51.515]]]";
JSONParser parser = new JSONParser();
try {
    JSONArray arrays3 = (JSONArray) parser.parse(lineStringJsonArray);
    JSONArray arrays2 = (JSONArray) arrays3.get(0);
    for (Object items : arrays2) {
        for (Object item : (JSONArray) items) {
            System.out.println((Double) item);
        }
    }
} catch (ParseException e) {
    e.printStackTrace();
}
Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88
  • Thanks for the help! By the way, could you please show me how to use a Pattern to extract those pair of latitude/longitude? Since I want to extract those pair of latitude/longitude, and add all of them into a list. – HaridingzLiu Aug 01 '15 at 05:53
2

If you want to decode this JSON string, I would advice you to use Jackson library (http://wiki.fasterxml.com/JacksonHome). You can find help on Google on how to import it and use it.

Then the class to use should look like this :

public class AwesomeClassName extends ArrayList<ArrayList<ArrayList<Double>>> {

}

Please ask if you need any help.

victorleduc
  • 232
  • 2
  • 9
  • Thank you very much, and sorry for that my expression might cause some confusion. Actually, I want to use pattern to extract those pairs of latitude/longitude, and add them to a list. – HaridingzLiu Aug 01 '15 at 05:51
0

Promoting reuse, I Prefer the answer from @ReutSharabani. However to the original question, to use Pattern, here is another solution:

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Play {
  static class Pair<T> { 
    T v1,v2; 
    Pair(T v1,T v2) { this.v1 = v1; this.v2 = v2; } 
    @Override public String toString() {
      return "(X: "+v1+", Y: "+v2+")";
    }
  }
  public static void main(String args[]) { 
    List<Pair<Double>> l = new ArrayList<>();
    Pattern p = Pattern.compile("\\[([^\\[\\],]*),([^\\[\\],]*)]");
    Matcher m = p.matcher("[[[0.093493,51.6037],[0.092077,51.6134],[0.075051,51.6179],[-0.247248,51.5166],[-0.259754,51.5235],[-0.28098,51.518],[-0.301457,51.515]]]");
    while (m.find()) {
      String v1 = m.group(1);
      String v2 = m.group(2);
      l.add(new Pair<>(Double.valueOf(v1),Double.valueOf(v2)));
    }
    l.stream().map(Pair::toString).forEach(System.out::println);
  }
}
YoYo
  • 9,157
  • 8
  • 57
  • 74