How do I get the columns into an csv file with opencsv from a JSONobject in Java? -


i writing little app go api, json data , fill csv file.

it works far, csv file, correct rows, instead of columns there still commas in text form in file.

i using opencsv.

private void writecsv(inputstream input, string name) throws ioexception {         string line;         bufferedreader reader = new bufferedreader(new inputstreamreader(input, "utf-8"));         stringbuilder text = new stringbuilder();         while ((line = reader.readline()) != null) {             text.append(line);         }         jsonarray docs = new jsonarray(text.tostring());         file file=new file(name + ".csv");         string csv = cdl.tostring(docs);         csvwriter writer = new csvwriter(new filewriter(file));         bufferedreader sreader = new bufferedreader(new stringreader(csv));         string csvline;         while ((csvline = sreader.readline()) != null) {             writer.writenext(csvline);         }         writer.close();         system.out.println("done"); } 

you need add column mapping strategy.

columnpositionmappingstrategy mappingstrategy = new columnpositionmappingstrategy(); 

check blog better understanding. http://www.javainterviewpoint.com/csvtobean-and-beantocsv-example-using-opencsv/


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -