Guys,
ObjectMapper mapper = new ObjectMapper();
obj = mapper.readValue(resp, DTestConnection.class);
Here is how the JSON looked like:
And here is how my Java class looked like:
public class DTestConnection {
String success;
String connected;
public String getSuccess() {
return success;
}
public void setSuccess(String success) {
this.success = success;
}
public String getConnected() {
return connected;
}
public void setConnected(String connected) {
this.connected = connected;
}
}
In one of my tasks, today I converted a JSON response (in form of a string) coming from REST service into a Java object so that I can then execute rest of my logic on that data.
It turned out to be really simple with JACKSON APIs. Here are two lines of code that you need to write:
ObjectMapper mapper = new ObjectMapper();
obj = mapper.readValue(resp, DTestConnection.class);
Here is how the JSON looked like:
{
"success": true,
"connected": true
}
And here is how my Java class looked like:
public class DTestConnection {
String success;
String connected;
public String getSuccess() {
return success;
}
public void setSuccess(String success) {
this.success = success;
}
public String getConnected() {
return connected;
}
public void setConnected(String connected) {
this.connected = connected;
}
}
No comments:
Post a Comment