如何用Java创建嵌套数组?twriteJsonField组件使用遇阻
Java实现指定JSON结构生成方案
针对你遇到的twriteJsonField组件无法处理多循环元素的问题,完全可以用Java结合Jackson库实现需求,以下是具体实现步骤:
1. 定义实体类
根据目标JSON结构,创建对应的Java实体类来映射数据:
CommentItem(对应comment_list中的子对象)
public class CommentItem { private String action; private String group; private String comment; public CommentItem(String action, String group, String comment) { this.action = action; this.group = group; this.comment = comment; } // 生成getter和setter方法,或使用Lombok的@Data注解简化 public String getAction() { return action; } public void setAction(String action) { this.action = action; } public String getGroup() { return group; } public void setGroup(String group) { this.group = group; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } }
LineItem(对应line_list中的子对象)
public class LineItem { private String line_year; private List<CommentItem> comment_list; public LineItem(String lineYear, List<CommentItem> commentList) { this.line_year = lineYear; this.comment_list = commentList; } // 生成getter和setter方法 public String getLine_year() { return line_year; } public void setLine_year(String line_year) { this.line_year = line_year; } public List<CommentItem> getComment_list() { return comment_list; } public void setComment_list(List<CommentItem> comment_list) { this.comment_list = comment_list; } }
Root(外层JSON根对象)
public class Root { private List<LineItem> line_list; public Root(List<LineItem> lineList) { this.line_list = lineList; } // 生成getter和setter方法 public List<LineItem> getLine_list() { return line_list; } public void setLine_list(List<LineItem> line_list) { this.line_list = line_list; } }
2. 数据转换与JSON序列化
编写主逻辑,将表格数据转换为目标结构并生成JSON:
import com.fasterxml.jackson.databind.ObjectMapper; import java.util.ArrayList; import java.util.List; public class JsonConverter { public static void main(String[] args) throws Exception { // 模拟表格输入数据,实际场景可替换为数据库查询、文件读取等逻辑 List<RowData> tableData = new ArrayList<>(); tableData.add(new RowData("2012", "Action1", "group1", "comment1")); tableData.add(new RowData("2020", "Action2", "group2", "comment2")); // 转换为目标JSON结构对应的对象 List<LineItem> lineItems = new ArrayList<>(); for (RowData row : tableData) { CommentItem commentItem = new CommentItem(row.getAction(), row.getGroup(), row.getComment()); List<CommentItem> commentList = new ArrayList<>(); commentList.add(commentItem); lineItems.add(new LineItem(row.getLineYear(), commentList)); } Root root = new Root(lineItems); // 序列化并输出格式化后的JSON ObjectMapper mapper = new ObjectMapper(); String formattedJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(root); System.out.println(formattedJson); } // 模拟表格行数据的实体类 static class RowData { private String lineYear; private String action; private String group; private String comment; public RowData(String lineYear, String action, String group, String comment) { this.lineYear = lineYear; this.action = action; this.group = group; this.comment = comment; } // 生成getter方法 public String getLineYear() { return lineYear; } public String getAction() { return action; } public String getGroup() { return group; } public String getComment() { return comment; } } }
3. 添加依赖
如果使用Maven管理项目,需在pom.xml中添加Jackson依赖:
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.15.2</version> <!-- 可替换为最新稳定版本 --> </dependency>
说明
- 该方案完全脱离
twriteJsonField组件限制,通过Java代码自由控制数据结构转换 - 若原始数据来自数据库、CSV等其他来源,仅需修改
tableData的填充逻辑即可适配 - Jackson库会自动处理JSON的格式化输出,与你要求的结构完全一致
内容的提问来源于stack exchange,提问作者Java BEE
相关产品推荐
相关产品推荐

