英文:
Convert JSON array of arrays to Java List of a specific class
问题
假设我有从源提取的JSON数据,格式如下:
{
"fileName" : "C:\\Users\\jgagnon\\sample_data\\PDM_BOM.xlsx",
"sheets" : [ {
"name" : "PDM_BOM",
"data" : [ [ "BRANCH", "PARENT ITEM NUMBER", "2ND ITEM NUMBER", "QUANTITY REQUIRED", "UNIT OF MEASURE", "ISSUE TYPE CODE", "LINE TYPE", "STOCK TYPE", "TYPE BOM", "LINE NUMBER", "OPERATING SEQUENCE", "EFFECTIVE FROM DATE", "EFFECTIVE THRU DATE", "DRAWING NUMBER", "UNIT COST", "SCRAP PERCENT" ],
[ "B20", "208E8840040", "5P884LPFSR2", 0.32031, "LB", "I", "S", "M", "M", 1.0, 10.0, "09/11/13", "12/31/40", null, 0.0, 0.0 ],
[ "B20", "208E8840168", "5P884LPFSR2", 1.36, "LB", "I", "S", "M", "M", 1.0, 10.0, "02/26/08", "12/31/40", null, 0.0, 0.0 ],
[ "B20", "208E8840172", "5P884LPFSR2", 1.3924, "LB", "I", "S", "M", "M", 1.0, 10.0, "02/26/08", "12/31/40", null, 0.0, 0.0 ],
[ "B20", "208E8840180", "5P884LPFSR2", 1.4565, "LB", "I", "S", "P", "M", 1.0, 10.0, "03/04/09", "12/31/40", null, 0.0, 0.0 ],
[ "B20", "21PPH150166", "8P315TPMRG", 1.39629, "LB", "I", "S", "M", "M", 1.0, 10.0, "03/05/18", "12/31/40", null, 0.0, 0.0 ] ],
"maxCols" : 16,
"maxRows" : 14996
} ]
}
我关心的信息位于data
元素中,它是一个表示工作表行的数组数组。第一行包含列标题。
现在,假设我有一个Java类BOM.java
,如下所示:
public class BOM {
private String branch;
private String parentItemNumber;
private String secondItemNumber;
private double quantityRequired;
private String unitOfMeasure;
private String issueTypeCode;
private String lineType;
private String stockType;
private String typeBom;
private double lineNumber;
private double operatingSequence;
private String effectiveFromDate;
private String effectiveThruDate;
private String drawingNumber;
private double unitCost;
private double scrapPercent;
// getters/setters omitted
}
是否有一种方法可以解析此JSON数据,以便得到BOM
对象的java.util.List
?
我至少认为,我需要在上面的类中添加一些Jackson的“JSON”注解,以帮助Jackson进行字段映射?
到目前为止,我使用了Jackson的ObjectMapper.readTree(File)
方法来导入数据。这提供了一个JsonNode
层次结构,我可以通过导航到其中的项。
我找到了一些建议,涉及使用Jackson的TypeFactory
或TypeReference
,但我不理解它们的工作原理,无法使它们对我起作用。
这是我目前正在使用的简单程序:
import java.io.File;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JSONObjectReader {
public static void main(String[] args)
throws JsonParseException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(new File("PDM_BOM.json"));
JsonNode sheets = root.get("sheets");
JsonNode sheet1 = sheets.get(0);
JsonNode dataNode = sheet1.get("data");
// skip first row
for (int i = 1; i < dataNode.size(); i++) {
JsonNode n = dataNode.get(i);
System.out.println(n);
}
}
}
英文:
Let's assume I have JSON data that has been extracted from a source in the following form:
{
"fileName" : "C:\\Users\\jgagnon\\sample_data\\PDM_BOM.xlsx",
"sheets" : [ {
"name" : "PDM_BOM",
"data" : [ [ "BRANCH", "PARENT ITEM NUMBER", "2ND ITEM NUMBER", "QUANTITY REQUIRED", "UNIT OF MEASURE", "ISSUE TYPE CODE", "LINE TYPE", "STOCK TYPE", "TYPE BOM", "LINE NUMBER", "OPERATING SEQUENCE", "EFFECTIVE FROM DATE", "EFFECTIVE THRU DATE", "DRAWING NUMBER", "UNIT COST", "SCRAP PERCENT" ],
[ "B20", "208E8840040", "5P884LPFSR2", 0.32031, "LB", "I", "S", "M", "M", 1.0, 10.0, "09/11/13", "12/31/40", null, 0.0, 0.0 ],
[ "B20", "208E8840168", "5P884LPFSR2", 1.36, "LB", "I", "S", "M", "M", 1.0, 10.0, "02/26/08", "12/31/40", null, 0.0, 0.0 ],
[ "B20", "208E8840172", "5P884LPFSR2", 1.3924, "LB", "I", "S", "M", "M", 1.0, 10.0, "02/26/08", "12/31/40", null, 0.0, 0.0 ],
[ "B20", "208E8840180", "5P884LPFSR2", 1.4565, "LB", "I", "S", "P", "M", 1.0, 10.0, "03/04/09", "12/31/40", null, 0.0, 0.0 ],
[ "B20", "21PPH150166", "8P315TPMRG", 1.39629, "LB", "I", "S", "M", "M", 1.0, 10.0, "03/05/18", "12/31/40", null, 0.0, 0.0 ] ],
"maxCols" : 16,
"maxRows" : 14996
} ]
}
The information I care about is in the data
element, which is an array of arrays representing the rows from a sheet. The first row contains the column headers.
Now, assume I have a Java class, BOM.java
as follows:
public class BOM {
private String branch;
private String parentItemNumber;
private String secondItemNumber;
private double quantityRequired;
private String unitOfMeasure;
private String issueTypeCode;
private String lineType;
private String stockType;
private String typeBom;
private double lineNumber;
private double operatingSequence;
private String effectiveFromDate;
private String effectiveThruDate;
private String drawingNumber;
private double unitCost;
private double scrapPercent;
// getters/setters omitted
}
Is there a way to parse this JSON data that results in a java.util.List
of BOM
objects?
I assume at the very least, that I need to add some Jackson "JSON" annotations to the class above to help Jackson with field mapping?
Up to this point I have used the Jackson ObjectMapper.readTree(File)
method to pull in the data. This provides a JsonNode
hierarchy which I can navigate to get to items.
I found some suggestions that referred to using a Jackson TypeFactory
or TypeReference
, but I don't understand how they work and couldn't get them to work for me.
Here is the simple program I'm currently using:
import java.io.File;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JSONObjectReader {
public static void main(String[] args)
throws JsonParseException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(new File("PDM_BOM.json"));
JsonNode sheets = root.get("sheets");
JsonNode sheet1 = sheets.get(0);
JsonNode dataNode = sheet1.get("data");
// skip first row
for (int i = 1; i < dataNode.size(); i++) {
JsonNode n = dataNode.get(i);
System.out.println(n);
}
}
}
答案1
得分: 1
你的列表结构几乎类似于CSV格式:一行列名,后面是许多行的值。经过搜索,我找不到任何Jackson内置的方法来处理这种结构,甚至没有一个方法可以解析列表并忽略第一个元素。
我认为你应该忽略第一个元素的原因是,如果你的目标是解析成一个POJO,而不是例如Map<String, String>
,那么处理它将会非常困难。这是因为如果第一行不固定,字段名称可能会更改,那么你的POJO将不再反映你正在解析的内容。最好只忽略标题,或者如果标题与预期不符,则抛出异常,专注于解析后续的数组。
我会这样做,编写一个自定义反序列化器,将List<List<String>>
转换为List<BOM>
。反序列化器应该:
- 获取原始的列表列表
- 抛弃(或验证)第一个条目
- 对于所有后续条目,将它们转换为
BOM
- 我会编写一个接受
List<String>
的BOM
构造函数来实现这一点,但你也可以在反序列化器中完成所有的转换。
- 我会编写一个接受
英文:
Your list structure is almost like a CSV: a row of column names, followed by many rows of values. Looking around, I couldn't find any Jackson built-ins to handle such a structure - not even one to parse a list while ignoring the first element.
The reason I think you should ignore the first element is that it will be very hard to deal with if your goal is to parse into a POJO and not, for example, a Map<String, String>
. This is because if the first row is not fixed, your field names may change, and then your POJO will no longer reflect what you're parsing. Better to just ignore the header, or throw an exception if it doesn't match what you expect, and focus on parsing the subsequent arrays.
The way I would do this is to write a Custom Deserializer for turning List<List<String>>
into List<BOM>
. The deserializer should:
- Get the raw list of lists
- Throw out (or verify) the first entry
- For all subsequent entries, convert them to
BOM
- I would do this by writing a
BOM
constructor that takes aList<String>
, but you could also do all the conversion in the deserializer.
- I would do this by writing a
答案2
得分: 1
这 应该 可以工作:
List<DTO> result = new ArrayList<>();
for (int i = 1; i < dataNode.size(); i++) {
JsonNode n = dataNode.get(i);
result.add(mapper.readValue(mapper.writeValueAsString(n), DTO.class));
}
注意这实际上相当丑陋 - 它解析了所有节点,然后将其写回JSON字符串,最后重新解析为DTO。更清晰的版本将需要处理格式不正确的第一行。
英文:
This should work:
List<DTO> result = new ArrayList<>();
for (int i = 1; i < dataNode.size(); i++) {
JsonNode n = dataNode.get(i);
result.add(mapper.readValue(mapper.writeValueAsString(n), DTO.class));
}
Note that it is really ugly - it parses all nodes and then writes back to a JSON string before finally re-parsing as a DTO. A cleaner version would require dealing with the malformed 1st row.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论