I have the following JSON file to deserialize
{
"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"
},
...
]}
I would like to deserialise the JSON file to something like an ArrayList so that I can calculate individual user's total profit by accessing a value of the array.
I have the following class as a wrapper;
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;
}
I also have the following class to store each attribute.
@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..
Finally, I have the following code in my main at the moment. However, it just stores the array of the objects as a whole and I cannot access an individual attribute of the row. The size of the array is just 1 and every data is in it.
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));
Output:
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, ....
How can I store each user's data into an array list and access it individually so that I can calculate the individual's total profit?
Rows rows = mapper.readValue(new File(ClientsRecordsPath), Rows.class);will produce correct result, no? Do you have one entry of 'Rows' object in your JSON file or many? ProbablyTypeReferencemay be useful here: fasterxml.github.io/jackson-core/javadoc/2.2.0/com/fasterxml/…