티스토리 뷰

반응형

15년 7월 기준.

<dependency>

<groupId>org.codehaus.jackson</groupId>

<artifactId>jackson-mapper-asl</artifactId>

<version>1.9.13</version>

</dependency>

ObjectMapper mapper = new ObjectMapper();

String json = "{\"name\":\"james\", \"age\":24, \"messages\" : [\"msg 1\",\"msg 2\",\"msg 3\"] }";

Map<String, Object> map = new HashMap<String, Object>();

map = mapper.readValue(json, new TypeReference<Map<String, String>>(){});

System.out.println(map);

System.out.println(map.get("name"));

ArrayList arr = (ArrayList)map.get("messages");

System.out.println(arr.get(2));

// json to map

ObjectMapper mapper = new ObjectMapper();

String json = "";

Map<String, Object> map = new HashMap<String, Object>();

map.put("name", "james");

map.put("age", 24);

List<Object> list = new ArrayList<Object>();

list.add("msg 1");

list.add("msg 2");

list.add("msg 3");

// json = mapper.writeValueAsString(map);

json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(map);

System.out.println(json);

// map to json

Map<String, Object> map = mapper.readValue(new File("c:\\test\\user.json"), new TypeReference<Map<String, Object>>() {});

mapper.writeValue(new File("c:\\test\\user.json"), map);

// json i/o

반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함