Converting an CSV file to a JSON object in Java

前端 未结 9 1689
太阳男子
太阳男子 2020-12-05 21:47

Is there an open source java library to convert a CSV (or XLS) file to a JSON object?

I tried using json.cdl, but somehow it does not seem to work for large CSV stri

9条回答
  •  情话喂你
    2020-12-05 22:21

    If your CSV is simple, then this is easy to write by hand - but CSV can include nasty edge cases with quoting, missing values, etc.

    • load the file using BufferedReader.readLine()
    • use String.split(",") to get the value from each line - NB this approach will only work correctly if your values don't have commas in!
    • write each value to the output using BufferedWriter
      • with the necessary JSON braces and quoting

    You might want to use a CSV library, then convert to JSON 'by hand'

提交回复
热议问题