title: Get data from JSON API and put into CSV in loop
body: <p>I am managed to get the data out of an API and put into a CSV, but I have problems to put the data into the CSV in a loop because right now it always overwrites it in the CSV. And the next problem is that the date does not get shown in the CSV in different fields. In the CSV it looks </p>
  
  <p>like this:</p>
  
  <p><img src="https://i.stack.imgur.com/PjGwA.png" alt="CSV picture"> </p>
  
  <p>and I want all the data like in my console: </p>
  
  <p><img src="https://i.stack.imgur.com/Xwe06.png" alt="console pic"></p>
  
  <p>my code right now: </p>
  
  <pre><code>    JSONArray jsonarr_1 = (JSONArray) jobj.get("infectedByRegion");
              //Get data for Results array
              for(int i=0;i&lt;jsonarr_1.size();i++)
              {
                  //Store the JSON objects in an array
                  //Get the index of the JSON object and print the values as per the index
                  JSONObject jsonobj_1 = (JSONObject)jsonarr_1.get(i);
                  //Store the JSON object in JSON array as objects (For level 2 array element i.e Address Components)
                  String str_data1 = (String) jsonobj_1.get("region");
                  Long str_data2 = (Long) jsonobj_1.get("infectedCount");
                  Long str_data3 = (Long) jsonobj_1.get("deceasedCount");
  
                  System.out.println(str_data1);
                  System.out.println("Infizierte: "+str_data2);
                  System.out.println("Tote: "+str_data3);
                  System.out.println("\n");
  
                     PrintWriter pw = null;
                      try {
                          pw = new PrintWriter(new File("C:/Users/stelz/OneDrive/Desktop/Corona Daten/28.04.2020.csv"));
                      } catch (FileNotFoundException e) {
                          e.printStackTrace();
                      }
                      StringBuilder builder = new StringBuilder();
                      String columnNamesList = "Bundesland,Infizierte,Tote";
                      // No need give the headers Like: id, Name on builder.append
                      builder.append(columnNamesList +"\n");
                      builder.append(str_data1+",");
                      builder.append(str_data2+",");
                      builder.append(str_data3);
                      builder.append('\n');
                      pw.write(builder.toString());
                      pw.close();
                      System.out.println("done!");
  
  
  
  
  
  
  
  
  
              }
              //Disconnect the HttpURLConnection stream
              conn.disconnect();
  </code></pre>