【问题标题】:convert DTO to JSON将 DTO 转换为 JSON
【发布时间】:2016-06-08 17:23:56
【问题描述】:

我创建了一个类,用于将 csv 文件转换为 dto,然后是 ojson 结构,但是当我转换为 json 时,我只得到了 csv 中的最后一行。

这是我的代码

public class TermParsing {

    private static final Logger log = Logger.getLogger(TermParsing.class.getName());
    Properties prop = new Properties();
    String data;
    //creating object for the dto class
    TermParsingDTO term = new TermParsingDTO();
    CsvReader read;
    JSONObject json = new JSONObject();
    HashMap hmap=new HashMap();

    //method to parse the csv file    
    void changeFileType() throws IOException {
        try {
            //to get the properties file from the source
            FileReader file = new FileReader("./config.txt");
            //loading the properties file
            prop.load(file);
            data = prop.getProperty("File");
            String path = prop.getProperty("Path");
            //to export the log message to the target
            FileHandler fh = new FileHandler(path);
            log.addHandler(fh);
            //to format the log messages
            SimpleFormatter format = new SimpleFormatter();
            fh.setFormatter(format);
            log.warning("FileNotFoundException may occur");
            //reading the input csv file
            read = new CsvReader(data);
            read.readHeaders();
            //to obtain the values from csv file 
            while (read.readRecord()) {
                term.setTerm_Name(Arrays.asList(read.get("Term Name")));
                term.setParent_category(Arrays.asList(read.get("Parent Category")));
                term.setShort_description(Arrays.asList(read.get("Short Description")));
                term.setLong_description(Arrays.asList(read.get("Long Description")));
                term.setStatus(Arrays.asList(read.get("Status")));
            }                
        } catch (FileNotFoundException ex) {
            Logger.getLogger(TermParsing.class.getName()).log(Level.SEVERE, "FileNotFoundException", ex);
        }
    }

在此方法中,我的 json 结构仅针对单个值创建

void convertToJson() throws IOException {            
    ObjectMapper map = null;
    String jsonInString;
    map = new ObjectMapper();
    jsonInString = map.writerWithDefaultPrettyPrinter().writeValueAsString(term);
    System.out.println(jsonInString);
}

【问题讨论】:

    标签: java json csv


    【解决方案1】:

    你不应该独立阅读所有行吗?

    TermParsingDTO term = new TermParsingDTO();
    

    这应该是:

    List<TermParsingDTO> terms = new ArrayList<>();
    

    在循环结束时:

    terms.add(term);
    

    那么您的所有记录都会在您的列表中。

    【讨论】:

    • 当它完成时,最后一个值被重复。 { "Term" : [ { "term_Name" : "TermBulk9", "parent_category" : "Parent Category>>Cat s", "short_description" : "Test Term" , "long_description" : "Term used for testing", "status " : "Standard" }, { "term_Name" : "TermBulk9", "parent_category" : "父类别>>Cat s", "short_description" : "测试术语", "long_description" : "用于测试的术语", " status" : "Standard" }, ] }所有值都不显示
    猜你喜欢
    • 2014-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 2021-12-20
    相关资源
    最近更新 更多