19

This is probably one of those questions where the title says it all.

I am quite fascinated by the ObjectMapper's readValue(file, class) method, found within the Jackson library which reads a JSON string from a file and assigns it to an object.

I'm curious if this is possible to do by simply getting JSON from a string and applying it to an object.

Some sort of alternative readValue() method, which takes a String, instead of a file, and assigns it to an object?

For instance, while the default readValue(file, class) method looks like this:

ObjectMapper mapper = new ObjectMapper(); Student student = mapper.readValue("C:\\student.json", Student.class); 

I was wondering if there was some method in Jackson, which allowed the following:

ObjectMapper mapper = new ObjectMapper(); Student student = mapper.readValue("{\"id\":100,\"firstName\":\"Adam\"}", Student.class); 

The second example takes a string and an object of a class while the first one takes a file and an object of a class.

I just want to cut out the middle man, in this case, the file.

Is this doable or does no such method exist within the constraints of Jackson?

4
  • 2
    Please read the javadoc... Commented Feb 8, 2014 at 0:41
  • Show your Student Class. Commented Feb 8, 2014 at 0:42
  • 1
    ObjectMapper#readValue(String, Class) exists. Did you miss it? Commented Feb 8, 2014 at 0:45
  • @MattBall Oh my God, it does. I'm such an idiot. Thank you never the less, friend. Commented Feb 8, 2014 at 0:47

2 Answers 2

26

Try this,

You can't create a new string like your doing.

 String string = "{\"id\":100,\"firstName\":\"Adam\"}"; Student student = mapper.readValue(string, Student.class); 
Sign up to request clarification or add additional context in comments.

4 Comments

Constructing an ObjectMapper is relatively expensive; I can't recommend doing it every time a method is called.
Yea, I got ride of that when I saw the reason why it was failing is OP didn't but his string in ""
Thank you @djc391, I will. Thank you both for not making me feel too horrid about such a dumb question.
I have a json which has inner Arrays and inner Arrays. Consider each array has an object. In this case how to map json with inner Arrays to POJO java objects.
5

And instead of handling errors in every mapper.readValue(json, Class) you can write a helper class which has another Generic method.

and use

String jsonString = "{\"id\":100,\"firstName\":\"Adam\"}"; Student student = JSONUtils.convertToObject(jsonString, Student.class); 

I'm returning null and fancying printing trace & checking null later on. You can handle error cases on your own way.

public class JSONUtils { public static String convertToJSON(Object object) { ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); String json; try { json = ow.writeValueAsString(object); } catch (JsonProcessingException e) { e.printStackTrace(); return convertToJSON(e); } return json; } public static <T> T convertToObject(Class<T> clazz, String jsonString) { try { ObjectMapper mapper = new ObjectMapper(); return (T) mapper.readValue(jsonString, clazz); } catch(Exception e) { e.printStackTrace(); return null; } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.