title: Loop through a list and create a geoJSON dynamically in Java
body: <p>I'm using simpleJSON in Java and I want to create a geojson. I would like to loop through a list that for each element of the list that has longtitude coords an Array would be created for the longtitude and latitude coords. The format of the geojson should be as follows:</p>
  
  <pre><code>{"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"}
                  }
      ]
  }
  </code></pre>
  
  <p>My problem is that when I'm looping through the list and use the <code>.put</code> for a <code>JSONArray</code> object every time that it changes the previous values instead of just adding the new ones so I end up with something that looks like this:</p>
  
  <pre><code>{"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"}
                  }
      ]
  }
  </code></pre>
  
  <p>Here is the code that I'm using:</p>
  
  <pre><code>public String toJson(ArrayList&lt;String&gt; 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&lt;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();
  }
  </code></pre>
  
  <p>Any ideas?Thanks!!</p>