Deserializing a json string using jackson

Rohit Bajaj
Rohit Bajaj used Ask the Experts™
I am using     private static final ObjectMapper mapper = new ObjectMapper();
and trying to do :
        mapper.readValue(value, List.class);
Its giving compile time error: cannot resolve method readValue...
Here my value will be a string liek = ["option1", "option2"]
a json array and i want to convert it to List<String> object.

How should i do it ?

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Prepress/OMS Specialist
Here's a link to a Stack Overflow question that seems to answer this very question:
Map JSON string array to List<String> using Jackson

There are actually two answers, and although the first is marked as the answer, I believe the second may be a more flexible solution long term.
String json = "[ \"abc\", \"def\" ]";
ObjectMapper mapper = new ObjectMapper();

List<String> list = mapper.readValue(json, TypeFactory.defaultInstance().constructCollectionType(List.class, String.class));

Open in new window

Without seeing more of your code I can't tell you exactly how to implement this, but it should be fairly straight forward.
Jim RiddlesPrepress/OMS Specialist

The solution answers the author's question.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial