如何操作Java GeoJSON / JSON?

时间:2017-02-15 21:46:12

标签: java json api geojson

我正在以JSON / GEOJSON的形式从API(Aquaplot)中检索数据。 这是一个如何存储数据的简单视图。 我想检索所有坐标并将它们存储在一个数组中。

如何操纵数据?

{   "type": "FeatureCollection",   "features": 
[
    {
      "type": "Feature",
      "properties": {
        "total_length": 558.49614719928,
        "seca_length": 0,
        "crossed": [
          "suez-canal"
        ]
      },
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [32.67333984375, 33.174341551002],
          [32.3423, 31.2228],
          [32.310393, 31.094417],
          [32.319995, 30.811504],
          [32.342453, 30.703486],
          [32.305385, 30.568682],
          [32.396751, 30.357018],
          [32.449684, 30.285923],
          [32.500598, 30.260175],
          [32.52428, 30.244705],
          [32.560229, 30.198274],
          [32.585092, 29.973555],
          [32.567552, 29.923606],
          [32.714583, 29.448333],
          [33.237083, 28.553278],
          [34.018333, 27.504556],
          [35.92529296875, 24.806681353852]
        ]
      }
    }   ] }

1 个答案:

答案 0 :(得分:0)

public static void main(String args[]) throws Exception
{
    //String url = "http://restcountries.eu/rest/v1/name/norway";
    String url = "     ";

    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    // optional default is GET
    con.setRequestMethod("GET");

    //add request header
    con.setRequestProperty("Authorization", "   ");
    con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'GET' request to URL : " + url);
    System.out.println("Response Code : " + responseCode);
    System.out.println(con.getInputStream());


    BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);

    }
    in.close();

    //print result
    System.out.println(response.toString());


    JSONObject jsonResponse=new JSONObject(response.toString());
    if(jsonResponse==null)
    {
    System.out.println("jsnresponse \n \n\n\n");
     throw new IllegalArgumentException("Books cannot be null");

    }



Fill the url and key .

response.tostring() is your response which further with the help pattern matcher you can parse and get the desired result.