저는 jackson 라이브러리를 처음 사용합니다. 정기적으로 json 파일에 쓸 데이터가 있습니다. 현재 사용한 모든 자습서가 파일을 덮어 씁니다.jackson 라이브러리를 사용하여 json 파일에 pojos를 추가하는 방법
0
A
답변
4
JSON을 처리하는 데 Jackson library을 사용합니다. ObjectMapper
클래스는 POJO : s를 JSON으로, 그 반대로도 변환 할 수 있습니다. 그 외에도 아래 예제와 같이 파일 쓰기를 처리하기 위해 java.nio.file.Files
클래스를 사용할 것입니다.
// First, define some POJO
public static class Pojo {
private final String content;
@JsonCreator
public Pojo(String content) {
this.content = content;
}
public String getContent() {
return content;
}
}
// This test simply illustrates file writing of JSON objects
@Test
public void testAppendToFile() throws IOException {
// The ObjectMapper is used to convert between Pojos and JSON (and vice versa)
final ObjectMapper mapper = new ObjectMapper();
// Convert a Pojo to JSON
final String json1 = mapper.writeValueAsString(new Pojo("This is the content #1"));
// Write it to the file myfile.json.
// The first time the file is created and the content is NOT appended
Files.write(new File("myfile.json").toPath(), Arrays.asList(json1), StandardOpenOption.CREATE);
// Convert another Pojo to JSON
final String json2 = mapper.writeValueAsString(new Pojo("This is the content #2"));
// Write to the file again.
// The second time the content is appended (due to StandardOpenOption.APPEND)
Files.write(new File("myfile.json").toPath(), Arrays.asList(json2), StandardOpenOption.APPEND);
// Read the file and verify that there are 2 lines
final List<String> lines = Files.readAllLines(new File("myfile.json").toPath());
Assert.assertEquals(2, lines.size());
}
관련 문제
- 1. 라이브러리를 make 파일에 추가하는 방법
- 2. PHP - JSON 파일에 추가하는 방법
- 3. jackson 라이브러리를 사용하여 JSON 문자열에서 값을 가져 오는 방법은 무엇입니까?
- 4. json 배열에 PHP를 사용하여 .json 파일에 추가하는 방법
- 5. Android JSON 파싱 (Jackson)
- 6. ob_start를 사용하여 파일에 추가하는 방법
- 7. json.net을 사용하여 기존 json 파일에 노드를 추가하는 방법
- 8. Jackson JSON @JsonFilter 주석이없는 필터
- 9. Jackson json 파서 예외
- 10. JSON Jackson 공유 참조
- 11. Json/Jackson 명명 규칙
- 12. jackson android를 사용하여 json을 구문 분석하는 방법
- 13. Jackson Json serialization
- 14. Jackson JSON 클래스 구조체
- 15. Jackson Jersey JSON
- 16. spring/jackson JSON 비 직렬화 수정 방법
- 17. xcode에 라이브러리를 추가하는 방법
- 18. Jackson JSON 접두사
- 19. Jackson Json deserialization보기
- 20. Json jackson mapping ask
- 21. Jackson : JSON 속성 오버라이드
- 22. Jackson Json, ByteBuffer를 마샬링
- 23. Java MVC에서 Java JSON 라이브러리를 사용하여 파싱합니다.
- 24. Jackson 2.0을 사용하여 JSON 객체의 배열을 파싱
- 25. jackson json을 사용하여 json 문자열에 속성 추가
- 26. 파일에 레코드를 추가하는 방법
- 27. Jackson JSON 파서 사용 : 복잡한 JSON?
- 28. 표준 라이브러리를 사용하여 파일에 쓰기
- 29. Jackson JSON 역 직렬화 목록
- 30. nlohmann json 라이브러리를 사용하여 json 중 double을 구문 분석하는 방법
잭슨 도서관 자습서를 참조하는 것이 도움이 될 수 있습니다. –