0

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?

1

2 Answers 2

2

Note that leading zeroes are not allowed, so this JSON will be invalid. We can use JSONLint to check this.

Also, ObjectMapper throws com.fasterxml.jackson.databind.JsonMappingException: Invalid numeric value: Leading zeroes not allowed.

After removing leading zeroes, you can do:

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

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

Output:

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)

While that works fine, I suggest to remove the wrapper class if it has no use. We can deserialize straight to a 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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. I thought I would need to create a wrapper class for this JSON structure... but It looks like I don't.
0

The code for parsing the json file will be:

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

as the root of json is represented by Rows class, not ArrayList

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.