Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert json to object using jackson

Tags:

java

json

jackson

I have to convert a json into an object using jackson. The class is like:

class Country {  
    int a;  
    int b;  
}  

And the json i am getting:

{"country":{"a":1,"b":1}}

But when i am trying to deserialize this its giving me following error

org.codehaus.jackson.map.JsonMappingException: Unrecognized field "country"    

If i remove "country", i am able to get the object.

Is there any way i can tell jackson to just ignore "country" from the json string?

Thanks in advance.

like image 387
parbi Avatar asked Jul 23 '26 23:07

parbi


1 Answers

This is the correct behavior of Jackson, the actual json representation of Country object should be without the top level country. If your json absolutely has the top level country attribute, a cleaner approach would be to use a wrapper Country class like this:

class WrapperCountry {  
   Country country;
}

this way the json representation should correctly deserialize to the WrapperCountry object and you can retrieve country from that.

like image 77
Biju Kunjummen Avatar answered Jul 25 '26 13:07

Biju Kunjummen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!