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);
Answer from Dan Ciborowski - MSFT on Stack OverflowTry this,
You can't create a new string like your doing.
String string = "{\"id\":100,\"firstName\":\"Adam\"}";
Student student = mapper.readValue(string, Student.class);
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;
}
}
}
java - How to parse a JSON string into JsonNode in Jackson? - Stack Overflow
How to parse JSON String to java object with jackson? - Stack Overflow
java - jackson json to Object parsing - Stack Overflow
java - Jackson Converting String to Object - Stack Overflow
Videos
A slight variation on Richards answer but readTree can take a string so you can simplify it to:
ObjectMapper mapper = new ObjectMapper();
JsonNode actualObj = mapper.readTree("{\"k1\":\"v1\"}");
You need to use an ObjectMapper:
ObjectMapper mapper = new ObjectMapper();
JsonFactory factory = mapper.getJsonFactory(); // since 2.1 use mapper.getFactory() instead
JsonParser jp = factory.createJsonParser("{\"k1\":\"v1\"}");
JsonNode actualObj = mapper.readTree(jp);
Further documentation about creating parsers can be found here.
You can try this online json to java pojo mapper, It produces the required result and it comes in handy for me many times.
http://pojo.sodhanalibrary.com/
Try it and if you face any more difficulties provide the complete json you receive, we can work out some thing else.
Create a POJO Response.java class which contains your fields, and getters/setters, create a standard constructor and one which contains your fields, and a toString method(). I show you an example with Annotations
define class like:
@JsonAutoDetect
@JsonSerialize
public class Response implements Serializable
then your fields with
@JsonProperty
private String parameterName;
and lastly your constructors f.e. like this
@JsonCreator
public Response(@JsonProperty("parameterName") String parameterName) {
this.parameterName = parameterName;
}
To convert your object in JSON with Jackson:
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json = ow.writeValueAsString(object);
I know this is old (and I am new to java), but I ran into the same problem. And the answers were not as clear to me as a newbie... so I thought I would add what I learned.
I used a third-party library to aid in the endeavor: org.codehaus.jackson
All of the downloads for this can be found here.
For base JSON functionality, you need to add the following jars to your project's libraries: jackson-mapper-asl and jackson-core-asl
Choose the version your project needs. (Typically you can go with the latest stable build).
Once they are imported in to your project's libraries, add the following import lines to your code:
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
With the java object defined and assigned values that you wish to convert to JSON and return as part of a RESTful web service
User u = new User();
u.firstName = "Sample";
u.lastName = "User";
u.email = "[email protected]";
ObjectMapper mapper = new ObjectMapper();
try {
// convert user object to json string and return it
return mapper.writeValueAsString(u);
}
catch (JsonGenerationException | JsonMappingException e) {
// catch various errors
e.printStackTrace();
}
The result should looks like this:
{"firstName":"Sample","lastName":"User","email":"[email protected]"}