当前位置: 首页 > 知识库问答 >
问题:

复杂Java对象到CSV

甄鹏云
2023-03-14

我试图从一个相当复杂的Java对象生成一个CSV文件。该对象是一个会话,具有一些属性,字符串和消息的列表,这些字符串和消息又具有一些属性,还有一个注释的列表,这些注释具有一些属性。

session类如下所示;

public class Session {

    private Long id;

    private Date startDate;

    private Date endDate;

    private List<Message> messages;

    private List<String> participants;

    public TweetSession() {
    }  

    public TweetSession(Date startDate, List<Message> messages, List<String>     participants) {
        this.startDate = startDate;
        this.messages = messages;
        this.participants = participants;
    }

    public Long getId() {
        return id;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public List<Message> getMessages() {
        return messages;
    }

    public void setMessage(List<Message> messages) {
        this.message = message;
    }

    public List<String> getParticipants() {
        return participants;
    }

    public void setParticipants(List<String> participants) {
        this.participants = participants;
    }
}

消息类如下所示;

public class Message {

    private Long id;

    private Session session;

    private Date date;

    private String participant;

    private String content;

    private List<Comment> comments;

    public Message() {
    }

    public Message(String participant, Session session, Date date, String content) {
        this.participant = participant;
        this.session = session;
        this.content = content;
        this.date = date;
        this.comments = new ArrayList<>();
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getParticipant() {
        return participant;
    }

    public void setParticipant(String participant) {
        this.participant = participant;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public List<Comment> getComments() {
        return comments;
    }

    public void setComments(List<Comment> comments) {
        this.comments = comments;
    }

    public void addComment(Comment comment) {
        this.comments.add(comment);
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public TweetSession getSession() {
        return session;
    }

    public void setSession(TweetSession session) {
        this.session = session;
    }
}
public class Comment {

    private Long id;

    private Message message;

    private String participant;

    private String message;

    private Date date;

    public Comment() {
    }

    public Comment(String participant, Message message, String content, Date date) {
        this.participant = participant;
        this.content = content;
        this.message = message;
        this.date = date;
    }

    public String getParticipant() {
        return participant;
    }

    public void setParticipant(String participant) {
        this.participant = participant;
    }

    public Message getMessage() {
        return message;
    }

    public void setMessage(Message message) {
        this.message = message;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}   
public void generateCSV(Session session, HttpServletResponse response) throws IOException {
    String csvFileName = "session.csv";
    response.setContentType("text/csv");

    String headerKey = "Content-Disposition";
    String headerValue = String.format("attachment; filename=\"%s\"",
            csvFileName);
    response.setHeader(headerKey, headerValue);

    ICsvBeanWriter csvWriter = new CsvBeanWriter(response.getWriter(),
            CsvPreference.STANDARD_PREFERENCE);

    // Generate header for the CSV
    Field fields[] = session.getClass().getDeclaredFields();
    String[] header = new String[fields.length];
    for (int i = 0; i < fields.length; i++) {
        header[i] = fields[i].getName();
    }

    csvWriter.writeHeader(header);

    // Generate CSV content from data
    csvWriter.write(session, header);

    csvWriter.close();
}
{
    "id": 22,
    "startDate": 1447368081000,
    "endDate": null,
    "messages": [
        {
            "id": 10,
            "date": 1447368159000,
            "participant": "1",
            "content": "This is a message",
            "comments": []
        },
        {
            "id": 11,
            "date": 1447368168000,
            "participant": "1",
            "content": "This is also a message",
            "comments": []
        },
        {
            "id": 12,
            "date": 1447368179000,
            "participant": "1",
            "content": "This is another message",
            "comments": [
                {
                    "id": 10,
                    "participant": "1",
                    "message": "This is a comment",
                    "date": 1447368227000
                },
                {
                    "id": 11,
                    "participant": "1",
                    "message": "This is also a comment",
                    "date": 1447368234000
                }
            ]
        }
    ],
    "participants": [
        "1",
        "23"
    ]
}

事实上,开始认为(单一的)CSV可能不是解决这个问题的最佳方法。

共有1个答案

柯昆
2023-03-14

您拥有的数据中有许多1:N依赖项,并不适合单个CSV文件

我曾使用或见过的方法:

>

  • 一个带有session自己的数据的“混合”CSV,即在第一列中包含IDstartdateenddate,然后有两列作为JSON打印的消息和参与者

    "123", "2015-11-17", "2015-11-18", "[{id: 345, date: ...}, {id: 789, date: ...}]", "[...]"
    
      null

  •  类似资料:
    • 我得到和错误当我执行 错误是: 未处理的异常:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期BEGIN_OBJECT但BEGIN_ARRAY在第1行第52列路径$. f 这是 json 响应 我有这个班级 和这个其他

    • POJO类: 控制器: JSON请求: 问题: 当我处理请求时,我收到一个空的TransactionDto对象。 System.out.println(transactionDto) TransactionDto(id=null,userName=null,apiKey=null,amount=0,accountNumber=null,expirationDate=null,cscCode=nul

    • 我想序列化Internet上的一个复杂Java对象。第一次,我使用Google Gson来序列化这个类。Gson提供了一种将对象序列化为JSON字符串并通过toJson和fromJson从JSON字符串反序列化到对象的简单方法。然而,JSON字符串并不是很紧凑,在序列化字节[]数组时会带来很大的开销。 我正在阅读Google协议缓冲区。根据教程,用户必须手动为每条消息编写. proto文件。协议缓

    • 我有一个gprsEvents列表,其中列表中的每个元素都是一个映射 ,如下所示。我需要: null 我开始考虑使用Java8Stream(groupingBy):gprsents.stream().collectors.groupingBy(Map->map.Get(“CallChargingID”).ToString())) 我现在坚持得到合适的结果,特别是在一个单一的地图和上面提到的字段的列表

    • 问题内容: 我有以下情况: 在我的Query.xml中,我以这种方式编写了插入内容: 阅读mybatis结果地图指南后,我尝试在mybatis-config.xml文件中添加以下行: 但我一直收到以下错误: 谁能告诉我该如何设置? 问题答案: 中的属性需要引用结果映射的名称,而不是Java类型: 但是,如果作为单独的表存储在数据库中,则不支持嵌套插入。您将需要在Java中调用两个插入。Result