2016-06-28 55 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); 
    } 
    }