0

I am trying to convert the below JSON into a Java object using ObjectMapper class in jackson api but i am getting an error. How my java class should look like?

[{
    "saleValue": 100,
    "priceEventTypeCode": 1,
    "startDate": "2016-03-23T18:00:00.0Z",
    "updateStoredValue": true,
    "autoRound": false,
    "priceChangeNumber": "tkt"
},
{
    "saleValue": 100,
    "priceEventTypeCode": 1,
    "startDate": "2016-03-23T18:00:00.0Z",
    "updateStoredValue": true,
    "autoRound": false,
    "priceChangeNumber": "tkt"
}]

If i have the below json i am able to convert it to java object.

public class Pid
{
    private String priceChangeNumber;
    private String startDate;
    private String autoRound;
    private String priceEventTypeCode;
    private String saleValue;
    private String updateStoredValue;

    // getter and setter functions removed here.
}

Json:

{
    "saleValue": 100,
    "priceEventTypeCode": 1,
    "startDate": "2016-03-23T18:00:00.0Z",
    "updateStoredValue": true,
    "autoRound": false,
    "priceChangeNumber": "tkt"
}
1

1 Answer 1

2

ObjectMapper support below method. Use them :)

    //To Array
    Pid[] pidArray = mapper.readValue(json, Pid[].class);

    //To List
    List<Pid> pidList1 = mapper.readValue(json, new TypeReference<List<Pid>>(){});

    //To List Another way
    List<Pid> pidList2 = mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, Pid.class));
Sign up to request clarification or add additional context in comments.

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.