import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
MyObject myObject = new MyObject("John", 25);
String jsonString = objectMapper.writeValueAsString(myObject);
System.out.println(jsonString);
}
}
class MyObject {
private String name;
private int age;
public MyObject(String name, int age) {
this.name = name;
this.age = age;
}
// getters and setters
}
{"name":"John","age":25}
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
String jsonString = "{\"name\":\"John\",\"age\":25}";
MyObject myObject = objectMapper.readValue(jsonString, MyObject.class);
System.out.println(myObject.getName());
System.out.println(myObject.getAge());
}
}
class MyObject {
private String name;
private int age;
public MyObject() {}
// getters and setters
}
John
25
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
String jsonString = "{\"name\":\"John\",\"age\":25,\"address\":{\"street\":\"123 Main St\",\"city\":\"New York\"}}";
Person person = objectMapper.readValue(jsonString, Person.class);
System.out.println(person.getName());
System.out.println(person.getAge());
System.out.println(person.getAddress().getStreet());
System.out.println(person.getAddress().getCity());
}
}
class Person {
private String name;
private int age;
private Address address;
public Person() {}
// getters and setters
}
class Address {
private String street;
private String city;
public Address() {}
// getters and setters
}
John
25
123 Main St
New York