- json-simple-1.1.1.jar 다운로드
- http://www.java2s.com/Code/Jar/j/Downloadjsonsimple111jar.htm
- jackson-databind-2.9.7.jar 다운로드
https://jar-download.com/artifacts/com.fasterxml.jackson.core/jackson-databind/2.9.7/source-code
Jackson 이란?
Java Object를 JSON으로 변환하거나 JSON을 Java Object로 변환하는데 사용할 수 있는 Java 라이브러리입니다.
Jackson Github - https://github.com/FasterXML/jackson
Jackson 특징
1.Stream API : 스트림 형식으로 데이터를 분석하고 생성하기 때문에 성능이 좋습니다.
2.Tree Model : XML의 DOM 처럼 Node 형태로 데이터를 다룰 수 있기 때문에 유연성이 좋습니다.
3.Data Binding : POJO 기반의 자바 객체들을 JSON으로 변환시킬 수 있습니다.
Maven 설정
jackson-databind 라이브러리는 jackson-core 및 jackson-annotation 라이브러리의 의존성을 포함하기 때문에 메이븐을 사용하는 경우 jackson-databind 라이브러리만 추가해주시면 됩니다.
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
MVNrepository : https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
package com.tychejin.study.json;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonExample01 {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
User user = setUser();
try {
// 객체를 JSON 타입의 파일로 변환
mapper.writeValue(new File("c:\\user.json"), user);
// 객체를 JSON 타입의 String으로 변환
String jsonInString01 = mapper.writeValueAsString(user);
System.out.println(jsonInString01);
// 객체를 JSON 타입의 String으로 변환 및 정렬
String jsonInString02 = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(user);
System.out.println(jsonInString02);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static User setUser() {
User user = new User();
user.setName("JSON");
user.setAge(10);
List<String> list = new ArrayList<String>();
list.add("JSON은 자바스크립트를 확장하여 만들어졌습니다.");
list.add("JSON은 자바스크립트 객체 표기법을 따릅니다.");
list.add("JSON은 사람과 기계가 모두 읽기 편하도록 고안되었습니다.");
list.add("JSON은 프로그래밍 언어와 운영체제에 독립적입니다.");
user.setMessages(list);
return user;
}
}
package com.tychejin.study.json;
import java.util.List;
public class User {
private String name;
private int age;
private List<String> messages;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<String> getMessages() {
return messages;
}
public void setMessages(List<String> messages) {
this.messages = messages;
}
@Override
public String toString() {
String result = "";
result = "[name:"+ name + ",age:"+ age +",messages:"+ messages +"]";
return result;
}
}
JSON을 Java Object로 변환
import java.io.File;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonExample02 {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
try {
// JSON 타입의 파일을 객체로 변환
User user01 = mapper.readValue(new File("c:\\user.json"), User.class);
System.out.println(user01);
String jsonInString = "{\"name\":\"JSON\",\"age\":10,\"messages\":[\"JSON은 자바스크립트를 확장하여 만들어졌습니다.\",\"JSON은 자바스크립트 객체 표기법을 따릅니다.\",\"JSON은 사람과 기계가 모두 읽기 편하도록 고안되었습니다.\",\"JSON은 프로그래밍 언어와 운영체제에 독립적입니다.\"]}";
// JSON 타입의 String을 객체로 변환
User user02 = mapper.readValue(jsonInString, User.class);
System.out.println(user02);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
출처 :
https://tychejin.tistory.com/134














댓글 ( 6)
댓글 남기기