繁体   English   中英

java中TSV数据如何转换为JSON Object

[英]How to convert TSV data to JSON Object in java

TSV 数据: 在此处输入图像描述

Required OutPut :
[{
candidates: {
id:"agent_4",
text = "can i get a confirmation code",
count = 2},
{
id:"agent_11",
text = "text_2",
count =3},
}]

我对 JQ 有类似的问题,但如何在 java 中实现这一点? 使用jq将TSV文件转换为多个JSON arrays

通过BufferedReader加载文件并使用JsonArray格式化您的 Json output

    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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM