1

Below is the json response i am converting to pojo using jackson -

{
    paymentType": [
        {
            "Monthly": ["Monthly", "Monthly"],
            "Prepaid": ["Prepaid", "Prepaid"]
        },
        ""
    ]
}

Code:

String rspString = "{\"paymentType\": [{\"Monthly\": [\"Monthly\",\"Monthly\"],\"Prepaid\": [\"Prepaid\",\"Prepaid\"]},\"\"]}";

JsonUtil jsonUtil = new JsonUtil();
PaymentTypeResponse PaymentTypeRsp = new PaymentTypeResponse();
PaymentTypeRsp = (PaymentTypeResponse) jsonUtil.Json2Object(rspString, PaymentTypeRsp);
System.out.println(PaymentTypeRsp.getPaymentType().size());

I am getting the below exception:

Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class com.test.sample.PaymentTypeRespons] from String value (''); no single-String constructor/factory method

There is a blank value in the response, how to handle those values while converting to pojo?

In the same way if null is there how to handle this case?

Any pointer would be appreciated.

Thanks in advance.

Here is the class:

public class PaymentType 
{
    @JsonProperty("Monthly")
    public List<String> monthly;
    @JsonProperty("Prepaid")
    public List<String> prepaid;

     //getter and setters
}

public PaymentTypeResponse 
{
    @JsonProperty("paymentType")
    public List<PaymentType> paymentType;

    //setters and Getters
}
1
  • 1
    please add PaymentTypeResponse class Commented Oct 23, 2016 at 5:35

2 Answers 2

1

If you're only receiving either null or the empty string instead of a JSON object that correctly represents your PaymentType class, it's enough to set the following deserialization feature in the ObjectMapper:

mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);

Now, the empty string will be coerced to null, and you won't have this problem any more.


A more general approach (though more complex to handle) would be to add a constructor that accepts a String argument to your PaymentType class:

public PaymentType() {
}

public PaymentType(String json) {
    // parse the json string here and initialize members
}

Within the constructor that receives a String argument, you can parse the string as you want.

Note that you also need to add the no-args constructor, so that Jackson can deserialize from valid JSON.

Sign up to request clarification or add additional context in comments.

Comments

0

Your json is incorrect according to your pojo classes. In your json, the 3rd element inside "paymentType" array is an empty string which should be an array or simply null.

Change your json from this -

{
    "paymentType": [{
        "Monthly": ["Monthly", "Monthly"],
        "Prepaid": ["Prepaid", "Prepaid"]
    }, ""]
}

to this -

{
    "paymentType": [{
        "Monthly": ["Monthly", "Monthly"],
        "Prepaid": ["Prepaid", "Prepaid"]
    }]
}

Json string -

 String rspString = "{\"paymentType\": [{\"Monthly\": [\"Monthly\",\"Monthly\"],\"Prepaid\": [\"Prepaid\",\"Prepaid\"]},null]}";

OR this -

{
    "paymentType": [{
        "Monthly": ["Monthly", "Monthly"],
        "Prepaid": ["Prepaid", "Prepaid"]
    }, null]
}

Json string -

String rspString = "{\"paymentType\": [{\"Monthly\": [\"Monthly\",\"Monthly\"],\"Prepaid\": [\"Prepaid\",\"Prepaid\"]}]}";

2 Comments

Hi thanks for your reply i cannot change the json it is developed as it is which holds empty value. based on that json i have written the class . i could not write the 3rd as it holds empty value ,
for this you have to write your own custom deserializer, this might be helpful - stackoverflow.com/questions/12933394/…

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.