Jackson 将对象数组反序列化为数组列表

Jackson Deserializing an array of objects into an array list

我有以下 JSON 文件要反序列化

{
    "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 的文件,以便我可以通过访问数组的值来计算单个用户的总利润。

我有以下 class 作为包装器;

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;
    }

我还有下面的class来存储每个属性。


@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
        "USER_ID",
        "COMMISSION",
        "SWAPS",
        "PROFIT",
        "COMMENT"
})
@Generated("jsonschema2pojo")
public class Row {
    @JsonProperty("USER_ID")
    private int userId;
    @JsonProperty("COMMISSION")
    private float commission;
    @JsonProperty("SWAPS")
    private float swaps;
    @JsonProperty("PROFIT")
    private float profit;
    @JsonProperty("COMMENT")
    private String comment;

    @JsonProperty("USER_ID")
    public int getUserId() {
        return userId;
    }

    @JsonProperty("USER_ID")
    public void setUserId(int userId) {
        this.userId = userId;
    }

    @JsonProperty("COMMISSION")
    public float getCommission() {
        return commission;
    }
//..setter/getter continue..

最后,现在我的主程序中有以下代码。但是,它只是将对象数组存储为一个整体,我无法访问该行的单个属性。 数组大小只有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));

输出:

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文件的代码为:

Rows rows = mapper.readValue(new File(ClientsRecordsPath), Rows.class);

因为 json 的根由行 class 表示,而不是 ArrayList

请注意不允许使用前导零,因此此 JSON 将无效。我们可以使用 JSONLint 来检查这个。

此外,ObjectMapper 抛出 com.fasterxml.jackson.databind.JsonMappingException: Invalid numeric value: Leading zeroes not allowed

删除前导零后,您可以:

ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);

Rows rowsArrayList = mapper.readValue(new File(ClientsRecordsPath), Rows.class);

输出:

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)

虽然效果很好,但我建议删除包装 class 如果它没有用。 我们可以直接反序列化为 List<Row>:

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)