【问题标题】:How to manipulate Java GeoJSON / JSON?如何操作 Java GeoJSON / JSON?
【发布时间】:2017-02-15 21:46:12
【问题描述】:

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

如何操作数据?

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

【问题讨论】:

  • 操纵是什么意思?您想以某种数据结构(例如deserialize 数据)加载您的JSON 数据吗?您可以显示为任何代码吗?请澄清您的问题。
  • 以上结果来自 API。它是 JSON 结果。我能够检索所有数据,但我只需要坐标部分来执行某种计算。更准确地说,我想以某种数据结构加载 JSON 数据。

标签: java json api geojson


【解决方案1】:
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.

【讨论】:

  • 谢谢。我现在可以检索 API 结果,但更准确地说,我想检索坐标部分。我该怎么做呢。我猜坐标在嵌套数组中。
  • 整个响应是一个 json 对象。您需要在其中搜索几何而不是 json 数组坐标。如果你给我网址和访问密钥,我可以帮助你
猜你喜欢
  • 1970-01-01
  • 2016-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-23
相关资源
最近更新 更多