import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonExample {
public static void main(String[] args) {
ObjectMapper objectMapper = new ObjectMapper();
try {
String jsonString = objectMapper.writeValueAsString(person);
System.out.println(jsonString);
} catch (Exception e) {
e.printStackTrace();
}
}
static class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
}
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
public class JsonExample {
public static void main(String[] args) {
ObjectMapper objectMapper = new ObjectMapper();
try {
Person person = objectMapper.readValue(jsonString, Person.class);
System.out.println(person.getName());
System.out.println(person.getAge());
} catch (IOException e) {
e.printStackTrace();
}
}
static class Person {
private String name;
private int age;
}
}