When I have a json below,
{
"test1": "test123",
"test2": {
"a": 1,
"b": 2
}
}
I want to map this to below class using Jackson library.
class TestClass {
String test1;
String test2;
}
But I can’t do this because test2 is an json object, not String. I want to map both test1 and test2 to string
String test1; // "test123"
String test2; // "{"a": 1, "b": 2}"
What should I do?
2
Answers
You should create java class with values that corresponds to you json object. In your case
test1
is String object andtest2
requires you to create new class that will hold two strings ‘a
‘ andb
. With your approach you will lose clear data access when converting two values into one String.Please refer to those links for more information
Use custom deserializer.
Test
Output