2017-02-15 32 views
0

我正在以JSON/GEOJSON的形式從API(Aquaplot)中檢索數據。 這是存儲數據的簡單視圖。 我想檢索所有的座標並將它們存儲在一個數組中。如何操作Java GeoJSON/JSON?

如何操作數據?

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

你說的操作是什麼意思?你想在某種數據結構中加載你的'JSON'數據(例如'反序列化數據)?你可以顯示任何代碼?請澄清你的問題。 – ventiseis

+0

以上結果來自API。它的JSON結果。我能夠檢索所有的數據,但我只需要座標部分來執行某種計算。更準確地說,我想在某種數據結構中加載JSON數據。 –

回答

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. 
+0

謝謝。我現在能夠檢索API結果,但更確切地說,我想檢索座標部分。 我如何做到這一點。我猜座標是嵌套的數組。 –

+0

整個響應是一個json對象。 其中你需要搜索幾何和比json數組的座標。 我可以幫助你,如果你給我的網址和訪問鍵 –