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);
Answer from Jean Logeart on Stack OverflowVideos
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]"}
It sounds like you just need the JSON in plain text so you can copy/paste or something. If so, you could set up something incredibly simple using Spring Boot and Jackson, map whatever JSON you're trying to copy to a URI using @RequestMapping("/"), and as long as you have your controller annotated with @RestController, whatever object you return in the method will automatically be parsed to a JSON when Jackson is on your classpath. Then just go to localhost in your browser and the request uri you specified and you'll have a plain text JSON as a response. Something like this:
@RestController
public class Controller {
@RequestMapping("/")
public SomeClass returnObjectInBrowser() {
SomeClass someClass = new SomeClass();
someClass.doStuff();
return someClass;
}
}
You can override the toString() method of the bean class to convert object to JSON string
@Override <br>
public String toString() {
String str = "";
// Converts object to json string using GSON
// Gson gson = new Gson();
// str = gson.toJson(this);
//Converts object to json string using Jaxson
ObjectMapper mapper = new ObjectMapper();
try {
str = mapper.writeValueAsString(this);
} catch (Exception exception) {
log.error(exception);
}
return str;
}