2016-06-28 18 views
0

JavaでsimpleJSONを使用しています。ジオジソンを作成します。 longtitudeの座標を持つリストの各要素について、longtitudeとlatitudeの座標の配列が作成されるというリストをループしたいと思います。次のように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"} 
       } 
    ] 
} 

私の問題は、私はちょうど追加するのではなく、JSONArrayオブジェクトのために、それは以前の値を変更するたびに.putをリストをループして使用していたときに新しいもの、私はこのようなもので終わるので:

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(); 
} 

任意のアイデア:ここ

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

は、私が使用しているコードです。 ?ありがとう!!

+1

新しい 'JSONObject'または' JSONArray'はループ内で再初期化する必要があります。そうでなければ同じ参照を使用しています。 –

答えて

0

問題は、featureというとgeometryというループ外の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); 
    } 
    } 
関連する問題