BufferedReader reader = new BufferedReader(new FileReader("data.tsv"));
String[] fieldNames = reader.readLine().split("\t");
List<JSONObject> rows = new ArrayList<>();
// Read tsv file line by line
String line;
while ((line = reader.readLine()) != null) {
String[] fields = line.split("\t");
JSONObject row = new JSONObject();
for (int i = 0; i < fieldNames.length; i++) {
row.put(fieldNames[i], fields[i]);
}
rows.add(row);
}
reader.close();
// Convert the list of rows to a JSON array
JSONArray array = new JSONArray(rows);
1条答案
按热度按时间jexiocij1#
通过BufferedReader加载文件,并使用JsonArray化Json输出