Java Jackson将对象数组反序列化为数组列表
我有以下JSON文件要反序列化Java Jackson将对象数组反序列化为数组列表,java,json,jackson,Java,Json,Jackson,我有以下JSON文件要反序列化 { "rows": [ { "USER_ID": 001, "COMMISSION": 0, "SWAPS": -1.87, "PROFIT": -73.39, "COMMENT": "
{
"rows":
[
{
"USER_ID": 001,
"COMMISSION": 0,
"SWAPS": -1.87,
"PROFIT": -73.39,
"COMMENT": "MAM|12345678|10020031"
},
{
"USER_ID": 002,
"COMMISSION": 0,
"SWAPS": 0,
"PROFIT": 12.23,
"COMMENT": "PAMM|12345678|10229501"
},
{
"USER_ID": 003,
"COMMISSION": 0,
"SWAPS": 0,
"PROFIT": 396.77,
"COMMENT": "PAMM|12345678|10229501"
},
...
]}
我希望将JSON文件反序列化为类似ArrayList的内容,以便通过访问数组的值来计算单个用户的总利润
我有以下类作为包装器
Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"rows"
})
@Generated("jsonschema2pojo")
public class Rows {
@JsonProperty("rows")
private ArrayList<Row> rows = null;
@JsonProperty("rows")
public ArrayList<Row> getRows() {
return rows;
}
@JsonProperty("rows")
public void setRows(ArrayList<Row> rows) {
this.rows = rows;
}
最后,目前我的主要代码中有以下代码。但是,它只是将对象数组作为一个整体存储,我无法访问行的单个属性。
数组的大小仅为1,所有数据都在其中
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
ArrayList<Rows> rowsArrayList = mapper.readValue(new File(ClientsRecordsPath), ArrayList.class);
//json array to array object
System.out.println("JSON array to Array objects...");
System.out.println(rowsArrayList.get(0));
如何将每个用户的数据存储到数组列表中并单独访问,以便计算个人的总利润?解析json文件的代码如下:
Rows rows = mapper.readValue(new File(ClientsRecordsPath), Rows.class);
由于json的根由Rows类而非ArrayList表示,请注意不允许前导零,因此此json将无效。我们可以用它来检查这个 另外,
ObjectMapper
抛出com.fasterxml.jackson.databind.JsonMappingException:无效数值:不允许前导零
删除前导零后,可以执行以下操作:
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
Rows rowsArrayList = mapper.readValue(new File(ClientsRecordsPath), Rows.class);
输出:
COMMENT=PAMM|123456|10314558}, {USER_ID=001, COMMISSION=0, SWAPS=0, PROFIT=13.57, COMMENT=PAMM|123456|10314558}, {USER_ID=002, COMMISSION=0, SWAPS=0, PROFIT=67.47, COMMENT=PAMM|123456|10314558}, {USER_ID=003, COMMISSION=0, SWAPS=0, PROFIT=202.41, COMMENT=PAMM|123456|10314558}, {USER_ID=004, COMMISSION=0, SWAPS=0, PROFIT=58.96, COMMENT=PAMM|123456|10314558}, {USER_ID=005, COMMISSION=0, SWAPS=0, PROFIT=6095, COMMENT=PAMM|123456|10314560}, {USER_ID=006, COMMISSION=0, ....
JSON array to Array objects...
Row(userId=1, commission=0.0, swaps=-1.87, profit=-73.39, comment=MAM|12345678|10020031)
Row(userId=2, commission=0.0, swaps=0.0, profit=12.23, comment=PAMM|12345678|10229501)
Row(userId=3, commission=0.0, swaps=0.0, profit=396.77, comment=PAMM|12345678|10229501)
虽然这样做很好,如果包装器类没有任何用处,我建议删除它。 我们可以直接反序列化到
列表
:
List rows=Arrays.asList(mapper.treeToValue(mapper.readTree(新文件(ClientsRecordsPath)).get(“rows”),Row[].class));
rows.forEach(System.out::println);
行(userId=1,佣金=0.0,掉期=1.87,利润=73.39,注释=MAM | 12345678 | 10020031)
行(userId=2,佣金=0.0,掉期=0.0,利润=12.23,注释=PAMM | 12345678 | 10229501)
行(userId=3,佣金=0.0,掉期=0.0,利润=396.77,注释=PAMM | 12345678 | 10229501)
Rows Rows=mapper.readValue(新文件(ClientsRecordsPath),Rows.class)代码>将产生正确的结果,否?您的JSON文件中有一个或多个“Rows”对象条目吗?可能TypeReference
在这里可能有用:非常感谢。我想我需要为这个JSON结构创建一个包装器类。。。但看起来我没有。
JSON array to Array objects...
Row(userId=1, commission=0.0, swaps=-1.87, profit=-73.39, comment=MAM|12345678|10020031)
Row(userId=2, commission=0.0, swaps=0.0, profit=12.23, comment=PAMM|12345678|10229501)
Row(userId=3, commission=0.0, swaps=0.0, profit=396.77, comment=PAMM|12345678|10229501)
List<Row> rows = Arrays.asList(mapper.treeToValue(mapper.readTree(new File(ClientsRecordsPath)).get("rows"), Row[].class));
rows.forEach(System.out::println);
Row(userId=1, commission=0.0, swaps=-1.87, profit=-73.39, comment=MAM|12345678|10020031)
Row(userId=2, commission=0.0, swaps=0.0, profit=12.23, comment=PAMM|12345678|10229501)
Row(userId=3, commission=0.0, swaps=0.0, profit=396.77, comment=PAMM|12345678|10229501)