If you have already done the method that returns a String, just put it in JSON format.
An alternative would be to generate a JSON, from an object, see below:
If you are using Maven in your project, you can include these two libraries as dependencies:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.1</version>
</dependency>
Or just download and include them in your project.
And the code would look something like this:
A class with the attributes
public class Person{
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
And your method:
ObjectMapper mapper = new ObjectMapper();
Person person = new Person();
person.setName("Name");
person.setAge(12);
try {
String out = mapper.writeValueAsString(person);
System.out.println(out);
} catch (JsonProcessingException e) {
e.printStackTrace();
}