ObjectMapper mapper = new ObjectMapper();
User user = new User();
//Object to JSON in file
mapper.writeValue(new File("c:\\user.json"), user);
//Object to JSON in String
String jsonInString = mapper.writeValueAsString(user);
ObjectMapper mapper = new ObjectMapper();
String jsonInString = "{'name' : 'mkyong'}";
//JSON from file to Object
User user = mapper.readValue(new File("c:\\user.json"), User.class);
//JSON from String to Object
User user = mapper.readValue(jsonInString, User.class);
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
User.java
package com.mkyong.json;
import java.util.List;
public class User {
private String name;
private int age;
private List<String> messages;
//getters and setters
}
Convert an user object into a JSON formatted string.
JacksonExample.java
package com.mkyong.json;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
public class JacksonExample {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
//For testing
User user = createDummyUser();
try {
//Convert object to JSON string and save into file directly
mapper.writeValue(new File("D:\\user.json"), user);
//Convert object to JSON string
String jsonInString = mapper.writeValueAsString(user);
System.out.println(jsonInString);
//Convert object to JSON string and pretty print
jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(user);
System.out.println(jsonInString);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static User createDummyUser(){
User user = new User();
user.setName("mkyong");
user.setAge(33);
List<String> msg = new ArrayList<>();
msg.add("hello jackson 1");
msg.add("hello jackson 2");
msg.add("hello jackson 3");
user.setMessages(msg);
return user;
}
}
output
//new json file is created in D:\\user.json"
{"name":"mkyong","age":33,"messages":["hello jackson 1","hello jackson 2","hello jackson 3"]}
{
"name" : "mkyong",
"age" : 33,
"messages" : [ "hello jackson 1", "hello jackson 2", "hello jackson 3" ]
}
package com.mkyong.json;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
public class JacksonExample {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
try {
// Convert JSON string from file to Object
User user = mapper.readValue(new File("G:\\user.json"), User.class);
System.out.println(user);
// Convert JSON string to Object
String jsonInString = "{\"age\":33,\"messages\":[\"msg 1\",\"msg 2\"],\"name\":\"mkyong\"}";
User user1 = mapper.readValue(jsonInString, User.class);
System.out.println(user1);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
output
User [name=mkyong, age=33, messages=[hello jackson 1, hello jackson 2, hello jackson 3]]
User [name=mkyong, age=33, messages=[msg 1, msg 2]]
https://github.com/FasterXML/jackson
https://mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/
https://soulduse.tistory.com/22
https://velog.io/@dion/what-is-POJO
좋은 글 감사합니다. 잘 보고 갑니다