2016-06-28 53 views
0

我在Java中使用simpleJSON,我想創建一個geojson。我想循環一個列表,對於具有長期座標的列表中的每個元素,將爲經度和緯度座標創建一個數組。在以GeoJSON格式應該如下:循環遍歷列表並在Java中動態創建geoJSON

{"type": "FeatureCollection", 
"crs": { 
    "type": "name", 
    "properties": { 
      "name": "ESPG:4326" 
      } 
    }, 
    "features":[ 

    { 
     "type":"Feature", 
     "geometry":{ 
       "type":"Point", 
       "coordinates":[55,55] 
       }, 
     "properties":{ 
       "desc":"something"} 
       } 
    ] 
},{ 
     "type":"Feature", 
     "geometry":{ 
       "type":"Point", 
       "coordinates":[49,32] 
       }, 
     "properties":{ 
       "desc":"something"} 
       } 
    ] 
} 

我的問題是,當我每次它改變了以前的值時循環通過列表,並使用.putJSONArray對象,而不只是添加新的,所以我最終的東西,看起來像這樣:

{"type": "FeatureCollection", 
"crs": { 
    "type": "name", 
    "properties": { 
      "name": "ESPG:4326" 
      } 
    }, 
    "features":[ 

    { 
     "type":"Feature", 
     "geometry":{ 
       "type":"Point", 
       "coordinates":[55,55] 
       }, 
     "properties":{ 
       "desc":"something"} 
       } 
    ] 
},{ 
     "type":"Feature", 
     "geometry":{ 
       "type":"Point", 
       "coordinates":[55,55] 
       }, 
     "properties":{ 
       "desc":"something"} 
       } 
    ] 
} 

下面是代碼,我使用:

public String toJson(ArrayList<String> array){ 
    JSONObject featureCollection = new JSONObject(); 
    featureCollection.put("type", "FeatureCollection"); 
    JSONObject properties = new JSONObject(); 
    properties.put("name", "ESPG:4326"); 
    JSONObject crs = new JSONObject(); 
    crs.put("type", "name"); 
    crs.put("properties", properties); 
    featureCollection.put("crs", crs); 

    JSONArray features = new JSONArray(); 
    JSONObject feature = new JSONObject(); 
    feature.put("type", "Feature"); 
    JSONObject geometry = new JSONObject(); 

    JSONAray JSONArrayCoord = new JSONArray(); 
    for(int i=0; i<array.length; i++){ 
    eachElement = array[i]; 
    if(eachElement.getLongtitude()!=null){ 
     JSONArrayCoord.add(0, eachElement.getLongtitude()); 
     JSONArrayCoord.add(1, eachElement.getLatitude()); 
     geometry.put("type", "Point"); 
     geometry.put("coordinates", JSONArrayCoord); 
     feature.put("geometry", geometry); 

     features.add(feature); 
     featureCollection.put("features", features); 
    } 
    } 
    return featureCollection.toString(); 
} 

任何想法?謝謝!!

+1

每個新的'JSONObject'或'JSONArray'都需要在循環中重新初始化,否則您使用的是相同的引用。 –

回答

0

問題是我正在初始化一個叫做feature,JSONArrayCoordgeometry的JSONObject的循環之外。下面是for循環的更正代碼:

for(int i=0; i<array.length; i++){ 
    eachElement = array[i]; 
    if(eachElement.getLongtitude()!=null){ 
     JSONObject geometry = new JSONObject(); 
     JSONArray JSONArrayCoord = new JSONArray(); 
     JSONObject newFeature = new JSONObject(); 

     JSONArrayCoord.add(0, eachElement.getLongtitude()); 
     JSONArrayCoord.add(1, eachElement.getLatitude()); 

     geometry.put("type", "Point"); 
     geometry.put("coordinates", JSONArrayCoord); 
     feature.put("geometry", geometry); 
     features.add(newFeature); 
     featureCollection.put("features", features); 
    } 
    }